1
/* $Id: sip_auth_parser.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjsip/sip_auth_parser.h>
21
#include <pjsip/sip_auth_msg.h>
22
#include <pjsip/sip_parser.h>
23
#include <pj/assert.h>
24
#include <pj/string.h>
25
#include <pj/except.h>
28
static pjsip_hdr* parse_hdr_authorization ( pjsip_parse_ctx *ctx );
29
static pjsip_hdr* parse_hdr_proxy_authorization ( pjsip_parse_ctx *ctx );
30
static pjsip_hdr* parse_hdr_www_authenticate ( pjsip_parse_ctx *ctx );
31
static pjsip_hdr* parse_hdr_proxy_authenticate ( pjsip_parse_ctx *ctx );
33
static void parse_digest_credential ( pj_scanner *scanner, pj_pool_t *pool,
34
pjsip_digest_credential *cred);
35
static void parse_pgp_credential ( pj_scanner *scanner, pj_pool_t *pool,
36
pjsip_pgp_credential *cred);
37
static void parse_digest_challenge ( pj_scanner *scanner, pj_pool_t *pool,
38
pjsip_digest_challenge *chal);
39
static void parse_pgp_challenge ( pj_scanner *scanner, pj_pool_t *pool,
40
pjsip_pgp_challenge *chal);
42
const pj_str_t pjsip_USERNAME_STR = { "username", 8 },
43
pjsip_REALM_STR = { "realm", 5},
44
pjsip_NONCE_STR = { "nonce", 5},
45
pjsip_URI_STR = { "uri", 3 },
46
pjsip_RESPONSE_STR = { "response", 8 },
47
pjsip_ALGORITHM_STR = { "algorithm", 9 },
48
pjsip_DOMAIN_STR = { "domain", 6 },
49
pjsip_STALE_STR = { "stale", 5},
50
pjsip_QOP_STR = { "qop", 3},
51
pjsip_CNONCE_STR = { "cnonce", 6},
52
pjsip_OPAQUE_STR = { "opaque", 6},
53
pjsip_NC_STR = { "nc", 2},
54
pjsip_TRUE_STR = { "true", 4},
55
pjsip_QUOTED_TRUE_STR = { "\"true\"", 6},
56
pjsip_FALSE_STR = { "false", 5},
57
pjsip_QUOTED_FALSE_STR = { "\"false\"", 7},
58
pjsip_DIGEST_STR = { "Digest", 6},
59
pjsip_QUOTED_DIGEST_STR = { "\"Digest\"", 8},
60
pjsip_PGP_STR = { "PGP", 3 },
61
pjsip_QUOTED_PGP_STR = { "\"PGP\"", 5 },
62
pjsip_MD5_STR = { "md5", 3 },
63
pjsip_QUOTED_MD5_STR = { "\"md5\"", 5},
64
pjsip_AUTH_STR = { "auth", 4},
65
pjsip_QUOTED_AUTH_STR = { "\"auth\"", 6 };
68
static void parse_digest_credential( pj_scanner *scanner, pj_pool_t *pool,
69
pjsip_digest_credential *cred)
71
pj_list_init(&cred->other_param);
76
pjsip_parse_param_imp(scanner, pool, &name, &value,
77
PJSIP_PARSE_REMOVE_QUOTE);
79
if (!pj_stricmp(&name, &pjsip_USERNAME_STR)) {
80
cred->username = value;
82
} else if (!pj_stricmp(&name, &pjsip_REALM_STR)) {
85
} else if (!pj_stricmp(&name, &pjsip_NONCE_STR)) {
88
} else if (!pj_stricmp(&name, &pjsip_URI_STR)) {
91
} else if (!pj_stricmp(&name, &pjsip_RESPONSE_STR)) {
92
cred->response = value;
94
} else if (!pj_stricmp(&name, &pjsip_ALGORITHM_STR)) {
95
cred->algorithm = value;
97
} else if (!pj_stricmp(&name, &pjsip_CNONCE_STR)) {
100
} else if (!pj_stricmp(&name, &pjsip_OPAQUE_STR)) {
101
cred->opaque = value;
103
} else if (!pj_stricmp(&name, &pjsip_QOP_STR)) {
106
} else if (!pj_stricmp(&name, &pjsip_NC_STR)) {
110
pjsip_param *p = PJ_POOL_ALLOC_T(pool, pjsip_param);
113
pj_list_insert_before(&cred->other_param, p);
117
if (!pj_scan_is_eof(scanner) && *scanner->curptr == ',')
118
pj_scan_get_char(scanner);
124
static void parse_pgp_credential( pj_scanner *scanner, pj_pool_t *pool,
125
pjsip_pgp_credential *cred)
127
PJ_UNUSED_ARG(scanner);
131
PJ_THROW(PJSIP_SYN_ERR_EXCEPTION);
134
static void parse_digest_challenge( pj_scanner *scanner, pj_pool_t *pool,
135
pjsip_digest_challenge *chal)
137
pj_list_init(&chal->other_param);
140
pj_str_t name, value;
142
pjsip_parse_param_imp(scanner, pool, &name, &value,
143
PJSIP_PARSE_REMOVE_QUOTE);
145
if (!pj_stricmp(&name, &pjsip_REALM_STR)) {
148
} else if (!pj_stricmp(&name, &pjsip_DOMAIN_STR)) {
149
chal->domain = value;
151
} else if (!pj_stricmp(&name, &pjsip_NONCE_STR)) {
154
} else if (!pj_stricmp(&name, &pjsip_OPAQUE_STR)) {
155
chal->opaque = value;
157
} else if (!pj_stricmp(&name, &pjsip_STALE_STR)) {
158
if (!pj_stricmp(&value, &pjsip_TRUE_STR) ||
159
!pj_stricmp(&value, &pjsip_QUOTED_TRUE_STR))
164
} else if (!pj_stricmp(&name, &pjsip_ALGORITHM_STR)) {
165
chal->algorithm = value;
168
} else if (!pj_stricmp(&name, &pjsip_QOP_STR)) {
172
pjsip_param *p = PJ_POOL_ALLOC_T(pool, pjsip_param);
175
pj_list_insert_before(&chal->other_param, p);
179
if (!pj_scan_is_eof(scanner) && *scanner->curptr == ',')
180
pj_scan_get_char(scanner);
186
static void parse_pgp_challenge( pj_scanner *scanner, pj_pool_t *pool,
187
pjsip_pgp_challenge *chal)
189
PJ_UNUSED_ARG(scanner);
193
PJ_THROW(PJSIP_SYN_ERR_EXCEPTION);
196
static void int_parse_hdr_authorization( pj_scanner *scanner, pj_pool_t *pool,
197
pjsip_authorization_hdr *hdr)
199
const pjsip_parser_const_t *pc = pjsip_parser_const();
201
if (*scanner->curptr == '"') {
202
pj_scan_get_quote(scanner, '"', '"', &hdr->scheme);
204
hdr->scheme.slen -= 2;
206
pj_scan_get(scanner, &pc->pjsip_TOKEN_SPEC, &hdr->scheme);
209
if (!pj_stricmp(&hdr->scheme, &pjsip_DIGEST_STR)) {
211
parse_digest_credential(scanner, pool, &hdr->credential.digest);
213
} else if (!pj_stricmp(&hdr->scheme, &pjsip_PGP_STR)) {
215
parse_pgp_credential( scanner, pool, &hdr->credential.pgp);
218
PJ_THROW(PJSIP_SYN_ERR_EXCEPTION);
221
pjsip_parse_end_hdr_imp( scanner );
224
static void int_parse_hdr_authenticate( pj_scanner *scanner, pj_pool_t *pool,
225
pjsip_www_authenticate_hdr *hdr)
227
const pjsip_parser_const_t *pc = pjsip_parser_const();
229
if (*scanner->curptr == '"') {
230
pj_scan_get_quote(scanner, '"', '"', &hdr->scheme);
232
hdr->scheme.slen -= 2;
234
pj_scan_get(scanner, &pc->pjsip_TOKEN_SPEC, &hdr->scheme);
237
if (!pj_stricmp(&hdr->scheme, &pjsip_DIGEST_STR)) {
239
parse_digest_challenge(scanner, pool, &hdr->challenge.digest);
241
} else if (!pj_stricmp(&hdr->scheme, &pjsip_PGP_STR)) {
243
parse_pgp_challenge(scanner, pool, &hdr->challenge.pgp);
246
PJ_THROW(PJSIP_SYN_ERR_EXCEPTION);
249
pjsip_parse_end_hdr_imp( scanner );
253
static pjsip_hdr* parse_hdr_authorization( pjsip_parse_ctx *ctx )
255
pjsip_authorization_hdr *hdr = pjsip_authorization_hdr_create(ctx->pool);
256
int_parse_hdr_authorization(ctx->scanner, ctx->pool, hdr);
257
return (pjsip_hdr*)hdr;
260
static pjsip_hdr* parse_hdr_proxy_authorization( pjsip_parse_ctx *ctx )
262
pjsip_proxy_authorization_hdr *hdr =
263
pjsip_proxy_authorization_hdr_create(ctx->pool);
264
int_parse_hdr_authorization(ctx->scanner, ctx->pool, hdr);
265
return (pjsip_hdr*)hdr;
268
static pjsip_hdr* parse_hdr_www_authenticate( pjsip_parse_ctx *ctx )
270
pjsip_www_authenticate_hdr *hdr =
271
pjsip_www_authenticate_hdr_create(ctx->pool);
272
int_parse_hdr_authenticate(ctx->scanner, ctx->pool, hdr);
273
return (pjsip_hdr*)hdr;
276
static pjsip_hdr* parse_hdr_proxy_authenticate( pjsip_parse_ctx *ctx )
278
pjsip_proxy_authenticate_hdr *hdr =
279
pjsip_proxy_authenticate_hdr_create(ctx->pool);
280
int_parse_hdr_authenticate(ctx->scanner, ctx->pool, hdr);
281
return (pjsip_hdr*)hdr;
285
PJ_DEF(pj_status_t) pjsip_auth_init_parser()
289
status = pjsip_register_hdr_parser( "Authorization", NULL,
290
&parse_hdr_authorization);
291
PJ_ASSERT_RETURN(status==PJ_SUCCESS, status);
292
status = pjsip_register_hdr_parser( "Proxy-Authorization", NULL,
293
&parse_hdr_proxy_authorization);
294
PJ_ASSERT_RETURN(status==PJ_SUCCESS, status);
295
status = pjsip_register_hdr_parser( "WWW-Authenticate", NULL,
296
&parse_hdr_www_authenticate);
297
PJ_ASSERT_RETURN(status==PJ_SUCCESS, status);
298
status = pjsip_register_hdr_parser( "Proxy-Authenticate", NULL,
299
&parse_hdr_proxy_authenticate);
300
PJ_ASSERT_RETURN(status==PJ_SUCCESS, status);
305
PJ_DEF(void) pjsip_auth_deinit_parser()