1 /*
2  * Digest credentials parser
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26  *
27  * History:
28  * -------
29  *
30  * 2003-03-15: Duplicate algorithm in dig_cred_t removed (janakj)
31  */
32 
33 
34 
35 #ifndef DIGEST_PARSER_H
36 #define DIGEST_PARSER_H
37 
38 #include "../../str.h"
39 
40 
41 /* Type of algorithm used */
42 typedef enum alg {
43 	ALG_UNSPEC = 0,   /* Algorithm parameter not specified */
44 	ALG_MD5 = 1,      /* MD5 - default value*/
45 	ALG_MD5SESS = 2,  /* MD5-Session */
46 	ALG_OTHER = 4     /* Unknown */
47 } alg_t;
48 
49 
50 /* Quality Of Protection used */
51 typedef enum qop_type {
52 	QOP_UNSPEC = 0,   /* QOP parameter not present in response */
53 	QOP_AUTH = 1,     /* Authentication only */
54 	QOP_AUTHINT = 2,  /* Authentication with integrity checks */
55 	QOP_OTHER = 4     /* Unknown */
56 } qop_type_t;
57 
58 
59 /* Algorithm structure */
60 struct algorithm {
61 	str alg_str;       /* The original string representation */
62 	alg_t alg_parsed;  /* Parsed value */
63 };
64 
65 
66 /* QOP structure */
67 struct qp {
68 	str qop_str;           /* The original string representation */
69 	qop_type_t qop_parsed; /* Parsed value */
70 };
71 
72 
73 /* Username structure */
74 struct username {
75 	str whole;        /* The whole username parameter value */
76 	str user;         /* username part only */
77 	str domain;       /* Domain part only */
78 };
79 
80 
81 /*
82  * Parsed digest credentials
83  */
84 typedef struct dig_cred {
85 	struct username username;   /* Username */
86 	str realm;                  /* Realm */
87 	str nonce;                  /* Nonce value */
88 	str uri;                    /* digest-uri, duplicated Request-URI of the Request-Line */
89 	str response;               /* Response string */
90 	struct algorithm alg;       /* Type of algorithm used */
91 	str cnonce;                 /* Cnonce value */
92 	str opaque;                 /* Opaque data string */
93 	struct qp qop;              /* Quality Of Protection */
94 	str nc;                     /* Nonce count parameter */
95 } dig_cred_t;
96 
97 
98 /*
99  * Macro to obtain the value of realm. The macro would first
100  * check if there is any @domain part in the username and if
101  * so, it will be returned as the value of realm. This hack is
102  * ofter used to protect realm using the digest (username parameter
103  * is protected by the response hash) and also to allow subscribers
104  * to specify a different domain part than the one in realm parameter
105  */
106 #define GET_REALM(cred)                                           \
107     (((cred)->username.domain.len && (cred)->username.domain.s) ? \
108      &(cred)->username.domain :                                   \
109      &(cred)->realm)
110 
111 
112 
113 /*
114  * Initialize a digest credentials structure
115  */
116 void init_dig_cred(dig_cred_t* _c);
117 
118 
119 /*
120  * We support Digest authentication only
121  *
122  * Returns:
123  *  0 - if everything is OK
124  * -1 - Error while parsing
125  *  1 - Unknown scheme
126  */
127 int parse_digest_cred(str* _s, dig_cred_t* _c);
128 
129 
130 /*
131  * Parse qop string
132  */
133 void parse_qop(struct qp* _q);
134 
135 #endif /* DIGEST_PARSER_H */
136