~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjsip/include/pjsip/sip_auth.h

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: sip_auth.h 3553 2011-05-05 06:14:19Z nanang $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
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.
10
 
 *
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.
15
 
 *
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 
19
 
 */
20
 
#ifndef __PJSIP_AUTH_SIP_AUTH_H__
21
 
#define __PJSIP_AUTH_SIP_AUTH_H__
22
 
 
23
 
/**
24
 
 * @file pjsip_auth.h
25
 
 * @brief SIP Authorization Module.
26
 
 */
27
 
 
28
 
#include <pjsip/sip_config.h>
29
 
#include <pjsip/sip_auth_msg.h>
30
 
 
31
 
PJ_BEGIN_DECL
32
 
 
33
 
/**
34
 
 * @addtogroup PJSIP_AUTH
35
 
 * @ingroup PJSIP_CORE
36
 
 * @brief Client and server side authentication framework.
37
 
 */
38
 
 
39
 
/**
40
 
 * @defgroup PJSIP_AUTH_API Authentication API's
41
 
 * @ingroup PJSIP_AUTH
42
 
 * @brief Structures and functions to perform authentication.
43
 
 * @{
44
 
 */
45
 
 
46
 
/** Length of digest string. */
47
 
#define PJSIP_MD5STRLEN 32
48
 
 
49
 
 
50
 
/** Type of data in the credential information in #pjsip_cred_info. */
51
 
typedef enum pjsip_cred_data_type
52
 
{
53
 
    PJSIP_CRED_DATA_PLAIN_PASSWD=0, /**< Plain text password.           */
54
 
    PJSIP_CRED_DATA_DIGEST      =1, /**< Hashed digest.                 */
55
 
 
56
 
    PJSIP_CRED_DATA_EXT_AKA     =16 /**< Extended AKA info is available */
57
 
 
58
 
} pjsip_cred_data_type;
59
 
 
60
 
/** Authentication's quality of protection (qop) type. */
61
 
typedef enum pjsip_auth_qop_type
62
 
{
63
 
    PJSIP_AUTH_QOP_NONE,            /**< No quality of protection. */
64
 
    PJSIP_AUTH_QOP_AUTH,            /**< Authentication. */
65
 
    PJSIP_AUTH_QOP_AUTH_INT,        /**< Authentication with integrity protection. */
66
 
    PJSIP_AUTH_QOP_UNKNOWN          /**< Unknown protection. */
67
 
} pjsip_auth_qop_type;
68
 
 
69
 
 
70
 
/**
71
 
 * Type of callback function to create authentication response.
72
 
 * Application can specify this callback in \a cb field of the credential info
73
 
 * (#pjsip_cred_info) and specifying PJSIP_CRED_DATA_DIGEST_CALLBACK as 
74
 
 * \a data_type. When this function is called, most of the fields in the 
75
 
 * \a auth authentication response will have been filled by the framework. 
76
 
 * Application normally should just need to calculate the response digest 
77
 
 * of the authentication response.
78
 
 *
79
 
 * @param pool      Pool to allocate memory from if application needs to.
80
 
 * @param chal      The authentication challenge sent by server in 401
81
 
 *                  or 401 response, in either Proxy-Authenticate or
82
 
 *                  WWW-Authenticate header.
83
 
 * @param cred      The credential that has been selected by the framework
84
 
 *                  to authenticate against the challenge.
85
 
 * @param auth      The authentication response which application needs to
86
 
 *                  calculate the response digest.
87
 
 *
88
 
 * @return          Application may return non-PJ_SUCCESS to abort the
89
 
 *                  authentication process. When this happens, the 
90
 
 *                  framework will return failure to the original function
91
 
 *                  that requested authentication.
92
 
 */
93
 
typedef pj_status_t (*pjsip_cred_cb)(pj_pool_t *pool,
94
 
                                     const pjsip_digest_challenge *chal,
95
 
                                     const pjsip_cred_info *cred,
96
 
                                     const pj_str_t *method,
97
 
                                     pjsip_digest_credential *auth);
98
 
 
99
 
 
100
 
/** 
101
 
 * This structure describes credential information. 
102
 
 * A credential information is a static, persistent information that identifies
103
 
 * username and password required to authorize to a specific realm.
104
 
 *
105
 
 * Note that since PJSIP 0.7.0.1, it is possible to make a credential that is
106
 
 * valid for any realms, by setting the realm to star/wildcard character,
107
 
 * i.e. realm = pj_str("*");.
108
 
 */
109
 
struct pjsip_cred_info
110
 
{
111
 
    pj_str_t    realm;          /**< Realm. Use "*" to make a credential that
112
 
                                     can be used to authenticate against any
113
 
                                     challenges.                            */
114
 
    pj_str_t    scheme;         /**< Scheme (e.g. "digest").                */
115
 
    pj_str_t    username;       /**< User name.                             */
116
 
    int         data_type;      /**< Type of data (0 for plaintext passwd). */
117
 
    pj_str_t    data;           /**< The data, which can be a plaintext 
118
 
                                     password or a hashed digest.           */
119
 
 
120
 
    /** Extended data */
121
 
    union {
122
 
        /** Digest AKA credential information. Note that when AKA credential
123
 
         *  is being used, the \a data field of this #pjsip_cred_info is
124
 
         *  not used, but it still must be initialized to an empty string.
125
 
         * Please see \ref PJSIP_AUTH_AKA_API for more information.
126
 
         */
127
 
        struct {
128
 
            pj_str_t      k;    /**< Permanent subscriber key.          */
129
 
            pj_str_t      op;   /**< Operator variant key.              */
130
 
            pj_str_t      amf;  /**< Authentication Management Field    */
131
 
            pjsip_cred_cb cb;   /**< Callback to create AKA digest.     */
132
 
        } aka;
133
 
 
134
 
    } ext;
135
 
};
136
 
 
137
 
/**
138
 
 * This structure describes cached value of previously sent Authorization
139
 
 * or Proxy-Authorization header. The authentication framework keeps a list
140
 
 * of this structure and will resend the same header to the same server
141
 
 * as long as the method, uri, and nonce stays the same.
142
 
 */
143
 
typedef struct pjsip_cached_auth_hdr
144
 
{
145
 
    /** Standard list member */
146
 
    PJ_DECL_LIST_MEMBER(struct pjsip_cached_auth_hdr);
147
 
 
148
 
    pjsip_method             method;    /**< To quickly see the method. */
149
 
    pjsip_authorization_hdr *hdr;       /**< The cached header.         */
150
 
 
151
 
} pjsip_cached_auth_hdr;
152
 
 
153
 
 
154
 
/**
155
 
 * This structure describes authentication information for the specified
156
 
 * realm. Each instance of this structure describes authentication "session"
157
 
 * between this endpoint and remote server. This "session" information is
158
 
 * usefull to keep information that persists for more than one challenge,
159
 
 * such as nonce-count and cnonce value.
160
 
 *
161
 
 * Other than that, this structure also keeps the last authorization headers
162
 
 * that have been sent in the cache list.
163
 
 */
164
 
typedef struct pjsip_cached_auth
165
 
{
166
 
    /** Standard list member */
167
 
    PJ_DECL_LIST_MEMBER(struct pjsip_cached_auth);
168
 
 
169
 
    pj_str_t                     realm;     /**< Realm.                     */
170
 
    pj_bool_t                    is_proxy;  /**< Server type (401/407)      */
171
 
    pjsip_auth_qop_type          qop_value; /**< qop required by server.    */
172
 
    unsigned                     stale_cnt; /**< Number of stale retry.     */
173
 
#if PJSIP_AUTH_QOP_SUPPORT
174
 
    pj_uint32_t                  nc;        /**< Nonce count.               */
175
 
    pj_str_t                     cnonce;    /**< Cnonce value.              */
176
 
#endif
177
 
    pjsip_www_authenticate_hdr  *last_chal; /**< Last challenge seen.       */
178
 
#if PJSIP_AUTH_HEADER_CACHING
179
 
    pjsip_cached_auth_hdr        cached_hdr;/**< List of cached header for
180
 
                                                 each method.               */
181
 
#endif
182
 
 
183
 
} pjsip_cached_auth;
184
 
 
185
 
 
186
 
/**
187
 
 * This structure describes client authentication session preference.
188
 
 * The preference can be set by calling #pjsip_auth_clt_set_prefs().
189
 
 */
190
 
typedef struct pjsip_auth_clt_pref
191
 
{
192
 
    /**
193
 
     * If this flag is set, the authentication client framework will
194
 
     * send an empty Authorization header in each initial request.
195
 
     * Default is no.
196
 
     */
197
 
    pj_bool_t   initial_auth;
198
 
 
199
 
    /**
200
 
     * Specify the algorithm to use when empty Authorization header 
201
 
     * is to be sent for each initial request (see above)
202
 
     */
203
 
    pj_str_t    algorithm;
204
 
 
205
 
} pjsip_auth_clt_pref;
206
 
 
207
 
 
208
 
/**
209
 
 * Duplicate a client authentication preference setting.
210
 
 *
211
 
 * @param pool      The memory pool.
212
 
 * @param dst       Destination client authentication preference.
213
 
 * @param src       Source client authentication preference.
214
 
 */
215
 
PJ_DECL(void) pjsip_auth_clt_pref_dup(pj_pool_t *pool,
216
 
                                      pjsip_auth_clt_pref *dst,
217
 
                                      const pjsip_auth_clt_pref *src);
218
 
 
219
 
 
220
 
/**
221
 
 * This structure describes client authentication sessions. It keeps
222
 
 * all the information needed to authorize the client against all downstream 
223
 
 * servers.
224
 
 */
225
 
typedef struct pjsip_auth_clt_sess
226
 
{
227
 
    pj_pool_t           *pool;          /**< Pool to use.                   */
228
 
    pjsip_endpoint      *endpt;         /**< Endpoint where this belongs.   */
229
 
    pjsip_auth_clt_pref  pref;          /**< Preference/options.            */
230
 
    unsigned             cred_cnt;      /**< Number of credentials.         */
231
 
    pjsip_cred_info     *cred_info;     /**< Array of credential information*/
232
 
    pjsip_cached_auth    cached_auth;   /**< Cached authorization info.     */
233
 
 
234
 
} pjsip_auth_clt_sess;
235
 
 
236
 
 
237
 
/**
238
 
 * Duplicate a credential info.
239
 
 *
240
 
 * @param pool      The memory pool.
241
 
 * @param dst       Destination credential.
242
 
 * @param src       Source credential.
243
 
 */
244
 
PJ_DECL(void) pjsip_cred_info_dup(pj_pool_t *pool,
245
 
                                  pjsip_cred_info *dst,
246
 
                                  const pjsip_cred_info *src);
247
 
 
248
 
/**
249
 
 * Compare two credential infos.
250
 
 *
251
 
 * @param cred1     The credential info to compare.
252
 
 * @param cred2     The credential info to compare.
253
 
 *
254
 
 * @return          0 if both credentials are equal.
255
 
 */
256
 
PJ_DECL(int) pjsip_cred_info_cmp(const pjsip_cred_info *cred1,
257
 
                                 const pjsip_cred_info *cred2);
258
 
 
259
 
 
260
 
/**
261
 
 * Type of function to lookup credential for the specified name.
262
 
 *
263
 
 * @param pool          Pool to initialize the credential info.
264
 
 * @param realm         Realm to find the account.
265
 
 * @param acc_name      Account name to look for.
266
 
 * @param cred_info     The structure to put the credential when it's found.
267
 
 *
268
 
 * @return              The function MUST return PJ_SUCCESS when it found
269
 
 *                      a correct credential for the specified account and
270
 
 *                      realm. Otherwise it may return PJSIP_EAUTHACCNOTFOUND
271
 
 *                      or PJSIP_EAUTHACCDISABLED.
272
 
 */
273
 
typedef pj_status_t pjsip_auth_lookup_cred( pj_pool_t *pool,
274
 
                                            const pj_str_t *realm,
275
 
                                            const pj_str_t *acc_name,
276
 
                                            pjsip_cred_info *cred_info );
277
 
 
278
 
/** Flag to specify that server is a proxy. */
279
 
#define PJSIP_AUTH_SRV_IS_PROXY     1
280
 
 
281
 
/**
282
 
 * This structure describes server authentication information.
283
 
 */
284
 
typedef struct pjsip_auth_srv
285
 
{
286
 
    pj_str_t                 realm;     /**< Realm to serve.                */
287
 
    pj_bool_t                is_proxy;  /**< Will issue 407 instead of 401  */
288
 
    pjsip_auth_lookup_cred  *lookup;    /**< Lookup function.               */
289
 
 
290
 
} pjsip_auth_srv;
291
 
 
292
 
 
293
 
/**
294
 
 * Initialize client authentication session data structure, and set the 
295
 
 * session to use pool for its subsequent memory allocation. The argument 
296
 
 * options should be set to zero for this PJSIP version.
297
 
 *
298
 
 * @param sess          The client authentication session.
299
 
 * @param endpt         Endpoint where this session belongs.
300
 
 * @param pool          Pool to use.
301
 
 * @param options       Must be zero.
302
 
 *
303
 
 * @return              PJ_SUCCESS on success.
304
 
 */
305
 
PJ_DECL(pj_status_t) pjsip_auth_clt_init( pjsip_auth_clt_sess *sess,
306
 
                                          pjsip_endpoint *endpt,
307
 
                                          pj_pool_t *pool, 
308
 
                                          unsigned options);
309
 
 
310
 
 
311
 
/**
312
 
 * Clone client initialization session. 
313
 
 *
314
 
 * @param pool          Pool to use.
315
 
 * @param sess          Structure to put the duplicated session.
316
 
 * @param rhs           The client session to be cloned.
317
 
 *
318
 
 * @return              PJ_SUCCESS on success;
319
 
 */
320
 
PJ_DECL(pj_status_t) pjsip_auth_clt_clone( pj_pool_t *pool,
321
 
                                           pjsip_auth_clt_sess *sess,
322
 
                                           const pjsip_auth_clt_sess *rhs);
323
 
 
324
 
/**
325
 
 * Set the credentials to be used during the session. This will duplicate 
326
 
 * the specified credentials using client authentication's pool.
327
 
 *
328
 
 * @param sess          The client authentication session.
329
 
 * @param cred_cnt      Number of credentials.
330
 
 * @param c             Array of credentials.
331
 
 *
332
 
 * @return              PJ_SUCCESS on success.
333
 
 */
334
 
PJ_DECL(pj_status_t) pjsip_auth_clt_set_credentials( pjsip_auth_clt_sess *sess,
335
 
                                                     int cred_cnt,
336
 
                                                     const pjsip_cred_info *c);
337
 
 
338
 
 
339
 
/**
340
 
 * Set the preference for the client authentication session.
341
 
 *
342
 
 * @param sess          The client authentication session.
343
 
 * @param p             Preference.
344
 
 *
345
 
 * @return              PJ_SUCCESS on success.
346
 
 */
347
 
PJ_DECL(pj_status_t) pjsip_auth_clt_set_prefs(pjsip_auth_clt_sess *sess,
348
 
                                              const pjsip_auth_clt_pref *p);
349
 
 
350
 
 
351
 
/**
352
 
 * Get the preference for the client authentication session.
353
 
 *
354
 
 * @param sess          The client authentication session.
355
 
 * @param p             Pointer to receive the preference.
356
 
 *
357
 
 * @return              PJ_SUCCESS on success.
358
 
 */
359
 
PJ_DECL(pj_status_t) pjsip_auth_clt_get_prefs(pjsip_auth_clt_sess *sess,
360
 
                                              pjsip_auth_clt_pref *p);
361
 
 
362
 
/**
363
 
 * Initialize new request message with authorization headers.
364
 
 * This function will put Authorization/Proxy-Authorization headers to the
365
 
 * outgoing request message. If caching is enabled (PJSIP_AUTH_HEADER_CACHING)
366
 
 * and the session has previously sent Authorization/Proxy-Authorization header
367
 
 * with the same method, then the same Authorization/Proxy-Authorization header
368
 
 * will be resent from the cache only if qop is not present. If the stack is 
369
 
 * configured to automatically generate next Authorization/Proxy-Authorization
370
 
 * headers (PJSIP_AUTH_AUTO_SEND_NEXT flag), then new Authorization/Proxy-
371
 
 * Authorization headers are calculated and generated when they are not present
372
 
 * in the case or if authorization session has qop.
373
 
 *
374
 
 * If both PJSIP_AUTH_HEADER_CACHING flag and PJSIP_AUTH_AUTO_SEND_NEXT flag
375
 
 * are not set, this function will do nothing. The stack then will only send
376
 
 * Authorization/Proxy-Authorization to respond 401/407 response.
377
 
 *
378
 
 * @param sess          The client authentication session.
379
 
 * @param tdata         The request message to be initialized.
380
 
 *
381
 
 * @return              PJ_SUCCESS if successfull.
382
 
 */
383
 
PJ_DECL(pj_status_t) pjsip_auth_clt_init_req( pjsip_auth_clt_sess *sess,
384
 
                                              pjsip_tx_data *tdata );
385
 
 
386
 
 
387
 
/**
388
 
 * Call this function when a transaction failed with 401 or 407 response.
389
 
 * This function will reinitialize the original request message with the
390
 
 * authentication challenge found in the response message, and add the
391
 
 * new authorization header in the authorization cache.
392
 
 *
393
 
 * Note that upon return the reference counter of the new transmit data
394
 
 * will be set to 1.
395
 
 *
396
 
 * @param sess          The client authentication session.
397
 
 * @param rdata         The response message containing 401/407 status.
398
 
 * @param old_request   The original request message, which will be re-
399
 
 *                      created with authorization info.
400
 
 * @param new_request   Pointer to receive new request message which
401
 
 *                      will contain all required authorization headers.
402
 
 *
403
 
 * @return              PJ_SUCCESS if new request can be successfully
404
 
 *                      created to respond all the authentication
405
 
 *                      challenges.
406
 
 */
407
 
PJ_DECL(pj_status_t) pjsip_auth_clt_reinit_req( pjsip_auth_clt_sess *sess,
408
 
                                                const pjsip_rx_data *rdata,
409
 
                                                pjsip_tx_data *old_request,
410
 
                                                pjsip_tx_data **new_request );
411
 
 
412
 
/**
413
 
 * Initialize server authorization session data structure to serve the 
414
 
 * specified realm and to use lookup_func function to look for the credential 
415
 
 * info. 
416
 
 *
417
 
 * @param pool          Pool used to initialize the authentication server.
418
 
 * @param auth_srv      The authentication server structure.
419
 
 * @param realm         Realm to be served by the server.
420
 
 * @param lookup        Account lookup function.
421
 
 * @param options       Options, bitmask of:
422
 
 *                      - PJSIP_AUTH_SRV_IS_PROXY: to specify that the server
423
 
 *                        will authorize clients as a proxy server (instead of
424
 
 *                        as UAS), which means that Proxy-Authenticate will 
425
 
 *                        be used instead of WWW-Authenticate.
426
 
 *
427
 
 * @return              PJ_SUCCESS on success.
428
 
 */
429
 
PJ_DECL(pj_status_t) pjsip_auth_srv_init( pj_pool_t *pool,
430
 
                                          pjsip_auth_srv *auth_srv,
431
 
                                          const pj_str_t *realm,
432
 
                                          pjsip_auth_lookup_cred *lookup,
433
 
                                          unsigned options );
434
 
 
435
 
 
436
 
/**
437
 
 * Request the authorization server framework to verify the authorization 
438
 
 * information in the specified request in rdata.
439
 
 *
440
 
 * @param auth_srv      The server authentication structure.
441
 
 * @param rdata         Incoming request to be authenticated.
442
 
 * @param status_code   When not null, it will be filled with suitable 
443
 
 *                      status code to be sent to the client.
444
 
 *
445
 
 * @return              PJ_SUCCESS if request is successfully authenticated.
446
 
 *                      Otherwise the function may return one of the
447
 
 *                      following error codes:
448
 
 *                      - PJSIP_EAUTHNOAUTH
449
 
 *                      - PJSIP_EINVALIDAUTHSCHEME
450
 
 *                      - PJSIP_EAUTHACCNOTFOUND
451
 
 *                      - PJSIP_EAUTHACCDISABLED
452
 
 *                      - PJSIP_EAUTHINVALIDREALM
453
 
 *                      - PJSIP_EAUTHINVALIDDIGEST
454
 
 */
455
 
PJ_DECL(pj_status_t) pjsip_auth_srv_verify( pjsip_auth_srv *auth_srv,
456
 
                                            pjsip_rx_data *rdata,
457
 
                                            int *status_code );
458
 
 
459
 
 
460
 
/**
461
 
 * Add authentication challenge headers to the outgoing response in tdata. 
462
 
 * Application may specify its customized nonce and opaque for the challenge, 
463
 
 * or can leave the value to NULL to make the function fills them in with 
464
 
 * random characters.
465
 
 *
466
 
 * @param auth_srv      The server authentication structure.
467
 
 * @param qop           Optional qop value.
468
 
 * @param nonce         Optional nonce value.
469
 
 * @param opaque        Optional opaque value.
470
 
 * @param stale         Stale indication.
471
 
 * @param tdata         The outgoing response message. The response must have
472
 
 *                      401 or 407 response code.
473
 
 *
474
 
 * @return              PJ_SUCCESS on success.
475
 
 */
476
 
PJ_DECL(pj_status_t) pjsip_auth_srv_challenge( pjsip_auth_srv *auth_srv,
477
 
                                               const pj_str_t *qop,
478
 
                                               const pj_str_t *nonce,
479
 
                                               const pj_str_t *opaque,
480
 
                                               pj_bool_t stale,
481
 
                                               pjsip_tx_data *tdata);
482
 
 
483
 
/**
484
 
 * Helper function to create MD5 digest out of the specified 
485
 
 * parameters.
486
 
 *
487
 
 * @param result        String to store the response digest. This string
488
 
 *                      must have been preallocated by caller with the 
489
 
 *                      buffer at least PJSIP_MD5STRLEN (32 bytes) in size.
490
 
 * @param nonce         Optional nonce.
491
 
 * @param nc            Nonce count.
492
 
 * @param cnonce        Optional cnonce.
493
 
 * @param qop           Optional qop.
494
 
 * @param uri           URI.
495
 
 * @param realm         Realm.
496
 
 * @param cred_info     Credential info.
497
 
 * @param method        SIP method.
498
 
 */
499
 
PJ_DECL(void) pjsip_auth_create_digest(pj_str_t *result,
500
 
                                       const pj_str_t *nonce,
501
 
                                       const pj_str_t *nc,
502
 
                                       const pj_str_t *cnonce,
503
 
                                       const pj_str_t *qop,
504
 
                                       const pj_str_t *uri,
505
 
                                       const pj_str_t *realm,
506
 
                                       const pjsip_cred_info *cred_info,
507
 
                                       const pj_str_t *method);
508
 
 
509
 
/**
510
 
 * @}
511
 
 */
512
 
 
513
 
 
514
 
 
515
 
PJ_END_DECL
516
 
 
517
 
 
518
 
#endif  /* __PJSIP_AUTH_SIP_AUTH_H__ */
519