Fix correct authentication behavior for artificial endpoint.
[asterisk/asterisk.git] / res / res_pjsip_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_pjsip.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/module.h"
26 #include "asterisk/strings.h"
27
28 /*** MODULEINFO
29         <depend>pjproject</depend>
30         <depend>res_pjsip</depend>
31         <support_level>core</support_level>
32  ***/
33
34 AO2_GLOBAL_OBJ_STATIC(entity_id);
35
36 /*!
37  * \brief Determine if authentication is required
38  *
39  * Authentication is required if the endpoint has at least one auth
40  * section specified
41  */
42 static int digest_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
43 {
44         RAII_VAR(struct ast_sip_endpoint *, artificial, ast_sip_get_artificial_endpoint(), ao2_cleanup);
45
46         return endpoint == artificial || AST_VECTOR_SIZE(&endpoint->inbound_auths) > 0;
47 }
48
49 static void auth_store_cleanup(void *data)
50 {
51         struct ast_sip_auth **auth = data;
52
53         ao2_cleanup(*auth);
54         ast_free(data);
55 }
56
57 /*!
58  * \brief Thread-local storage for \ref ast_sip_auth
59  *
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
66  * addition.
67  *
68  * Unfortunately, the data we actually \b need is the
69  * \ref ast_sip_auth we are currently observing. So we have two
70  * choices:
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.
78  *
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.
82  */
83 AST_THREADSTORAGE_CUSTOM(auth_store, NULL, auth_store_cleanup);
84
85 /*!
86  * \brief Store authentication information in thread-local storage
87  */
88 static int store_auth(struct ast_sip_auth *auth)
89 {
90         struct ast_sip_auth **pointing;
91         pointing = ast_threadstorage_get(&auth_store, sizeof(pointing));
92         if (!pointing || *pointing) {
93                 return -1;
94         }
95
96         ao2_ref(auth, +1);
97         *pointing = auth;
98         return 0;
99 }
100
101 /*!
102  * \brief Remove authentication information from thread-local storage
103  */
104 static int remove_auth(void)
105 {
106         struct ast_sip_auth **pointing;
107         pointing = ast_threadstorage_get(&auth_store, sizeof(pointing));
108         if (!pointing) {
109                 return -1;
110         }
111
112         ao2_cleanup(*pointing);
113         *pointing = NULL;
114         return 0;
115 }
116
117 /*!
118  * \brief Retrieve authentication information from thread-local storage
119  */
120 static struct ast_sip_auth *get_auth(void)
121 {
122         struct ast_sip_auth **auth;
123         auth = ast_threadstorage_get(&auth_store, sizeof(auth));
124         if (auth && *auth) {
125                 ao2_ref(*auth, +1);
126                 return *auth;
127         }
128         return NULL;
129 }
130
131 /*!
132  * \brief Lookup callback for authentication verification
133  *
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.
138  *
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
145  */
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)
148 {
149         RAII_VAR(struct ast_sip_auth *, auth, get_auth(), ao2_cleanup);
150         if (!auth) {
151                 return PJSIP_SC_FORBIDDEN;
152         }
153
154         if (auth->type == AST_SIP_AUTH_TYPE_ARTIFICIAL) {
155                 return PJSIP_SC_FORBIDDEN;
156         }
157
158         if (pj_strcmp2(realm, auth->realm)) {
159                 return PJSIP_SC_FORBIDDEN;
160         }
161         if (pj_strcmp2(acc_name, auth->auth_user)) {
162                 return PJSIP_SC_FORBIDDEN;
163         }
164
165         pj_strdup2(pool, &info->realm, auth->realm);
166         pj_strdup2(pool, &info->username, auth->auth_user);
167
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;
172                 break;
173         case AST_SIP_AUTH_TYPE_MD5:
174                 pj_strdup2(pool, &info->data, auth->md5_creds);
175                 info->data_type = PJSIP_CRED_DATA_DIGEST;
176                 break;
177         default:
178                 return PJSIP_SC_FORBIDDEN;
179         }
180         return PJ_SUCCESS;
181 }
182
183 /*!
184  * \brief Calculate a nonce
185  *
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
188  * of our challenges.
189  *
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.
194  *
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
198  */
199 static int build_nonce(struct ast_str **nonce, const char *timestamp, const pjsip_rx_data *rdata, const char *realm)
200 {
201         struct ast_str *str = ast_str_alloca(256);
202         RAII_VAR(char *, eid, ao2_global_obj_ref(entity_id), ao2_cleanup);
203         char hash[32];
204
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));
211
212         ast_str_append(nonce, 0, "%s/%s", timestamp, hash);
213         return 0;
214 }
215
216 /*!
217  * \brief Ensure that a nonce on an incoming request is sane.
218  *
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
227  */
228 static int check_nonce(const char *candidate, const pjsip_rx_data *rdata, const struct ast_sip_auth *auth)
229 {
230         char *copy = ast_strdupa(candidate);
231         char *timestamp = strsep(&copy, "/");
232         int timestamp_int;
233         time_t now = time(NULL);
234         struct ast_str *calculated = ast_str_alloca(64);
235
236         if (!copy) {
237                 /* Clearly a bad nonce! */
238                 return 0;
239         }
240
241         if (sscanf(timestamp, "%30d", &timestamp_int) != 1) {
242                 return 0;
243         }
244
245         if ((int) now - timestamp_int > auth->nonce_lifetime) {
246                 return 0;
247         }
248
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)) {
252                 return 0;
253         }
254         return 1;
255 }
256
257 static int find_challenge(const pjsip_rx_data *rdata, const struct ast_sip_auth *auth)
258 {
259         struct pjsip_authorization_hdr *auth_hdr = (pjsip_authorization_hdr *) &rdata->msg_info.msg->hdr;
260         int challenge_found = 0;
261         char nonce[64];
262
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)) {
266                         challenge_found = 1;
267                         break;
268                 }
269         }
270
271         return challenge_found;
272 }
273
274 /*!
275  * \brief Common code for initializing a pjsip_auth_srv
276  */
277 static void setup_auth_srv(pj_pool_t *pool, pjsip_auth_srv *auth_server, const char *realm)
278 {
279         pj_str_t realm_str;
280         pj_cstr(&realm_str, realm);
281
282         pjsip_auth_srv_init(pool, auth_server, &realm_str, digest_lookup, 0);
283 }
284
285 /*!
286  * \brief Result of digest verification
287  */
288 enum digest_verify_result {
289         /*! Authentication credentials incorrect */
290         AUTH_FAIL,
291         /*! Authentication credentials correct */
292         AUTH_SUCCESS,
293         /*! Authentication credentials correct but nonce mismatch */
294         AUTH_STALE,
295         /*! Authentication credentials were not provided */
296         AUTH_NOAUTH,
297 };
298
299 /*!
300  * \brief astobj2 callback for verifying incoming credentials
301  *
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
307  */
308 static int verify(struct ast_sip_auth *auth, pjsip_rx_data *rdata, pj_pool_t *pool)
309 {
310         pj_status_t authed;
311         int response_code;
312         pjsip_auth_srv auth_server;
313         int stale = 0;
314
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.
318                  */
319                 stale = 1;
320         }
321
322         setup_auth_srv(pool, &auth_server, auth->realm);
323
324         store_auth(auth);
325
326         authed = pjsip_auth_srv_verify(&auth_server, rdata, &response_code);
327
328         remove_auth();
329
330         if (authed == PJ_SUCCESS) {
331                 if (stale) {
332                         return AUTH_STALE;
333                 } else {
334                         return AUTH_SUCCESS;
335                 }
336         }
337
338         if (authed == PJSIP_EAUTHNOAUTH) {
339                 return AUTH_NOAUTH;
340         }
341
342         return AUTH_FAIL;
343 }
344
345 /*!
346  * \brief astobj2 callback for adding digest challenges to responses
347  *
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
352  */
353 static void challenge(const char *realm, pjsip_tx_data *tdata, const pjsip_rx_data *rdata, int is_stale)
354 {
355         pj_str_t qop;
356         pj_str_t pj_nonce;
357         pjsip_auth_srv auth_server;
358         struct ast_str *nonce = ast_str_alloca(256);
359         char time_buf[32];
360         time_t timestamp = time(NULL);
361         snprintf(time_buf, sizeof(time_buf), "%d", (int) timestamp);
362
363         build_nonce(&nonce, time_buf, rdata, realm);
364
365         setup_auth_srv(tdata->pool, &auth_server, realm);
366
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);
370 }
371
372 /*!
373  * \brief Check authentication using Digest scheme
374  *
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.
378  *
379  * \see ast_sip_check_authentication
380  */
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)
383 {
384         struct ast_sip_auth **auths;
385         enum digest_verify_result *verify_res;
386         enum ast_sip_check_auth_result res;
387         int i;
388         int failures = 0;
389         size_t auth_size;
390
391         RAII_VAR(struct ast_sip_endpoint *, artificial_endpoint,
392                  ast_sip_get_artificial_endpoint(), ao2_cleanup);
393
394         auth_size = AST_VECTOR_SIZE(&endpoint->inbound_auths);
395
396         auths = ast_alloca(auth_size * sizeof(*auths));
397         verify_res = ast_alloca(auth_size * sizeof(*verify_res));
398
399         if (!auths) {
400                 return AST_SIP_AUTHENTICATION_ERROR;
401         }
402
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;
407                 goto cleanup;
408         }
409
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");
413                 }
414                 verify_res[i] = verify(auths[i], rdata, tdata->pool);
415                 if (verify_res[i] == AUTH_SUCCESS) {
416                         res = AST_SIP_AUTHENTICATION_SUCCESS;
417                         goto cleanup;
418                 }
419                 if (verify_res[i] == AUTH_FAIL) {
420                         failures++;
421                 }
422         }
423
424         for (i = 0; i < auth_size; ++i) {
425                 challenge(auths[i]->realm, tdata, rdata, verify_res[i] == AUTH_STALE);
426         }
427
428         if (failures == auth_size) {
429                 res = AST_SIP_AUTHENTICATION_FAILED;
430         } else {
431                 res = AST_SIP_AUTHENTICATION_CHALLENGE;
432         }
433
434 cleanup:
435         ast_sip_cleanup_auths(auths, auth_size);
436         return res;
437 }
438
439 static struct ast_sip_authenticator digest_authenticator = {
440         .requires_authentication = digest_requires_authentication,
441         .check_authentication = digest_check_auth,
442 };
443
444 static int build_entity_id(void)
445 {
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);
448
449         if (!uu || !eid) {
450                 return -1;
451         }
452
453         ast_uuid_to_str(uu, eid, AST_UUID_STR_LEN);
454         ao2_global_obj_replace_unref(entity_id, eid);
455         return 0;
456 }
457
458 static int reload_module(void)
459 {
460         if (build_entity_id()) {
461                 return -1;
462         }
463         return 0;
464 }
465
466 static int load_module(void)
467 {
468         if (build_entity_id()) {
469                 return AST_MODULE_LOAD_DECLINE;
470         }
471         if (ast_sip_register_authenticator(&digest_authenticator)) {
472                 ao2_global_obj_release(entity_id);
473                 return AST_MODULE_LOAD_DECLINE;
474         }
475         return AST_MODULE_LOAD_SUCCESS;
476 }
477
478 static int unload_module(void)
479 {
480         ast_sip_unregister_authenticator(&digest_authenticator);
481         ao2_global_obj_release(entity_id);
482         return 0;
483 }
484
485 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP authentication resource",
486                 .load = load_module,
487                 .unload = unload_module,
488                 .reload = reload_module,
489                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
490 );