authorize.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2012 Smile Communications, [email protected]
  5. * Copyright (C) 2012 Smile Communications, [email protected]
  6. *
  7. * The initial version of this code was written by Dragos Vingarzan
  8. * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
  9. * Fruanhofer Institute. It was and still is maintained in a separate
  10. * branch of the original SER. We are therefore migrating it to
  11. * Kamailio/SR and look forward to maintaining it from here on out.
  12. * 2011/2012 Smile Communications, Pty. Ltd.
  13. * ported/maintained/improved by
  14. * Jason Penton (jason(dot)penton(at)smilecoms.com and
  15. * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
  16. * effort to add full IMS support to Kamailio/SR using a new and
  17. * improved architecture
  18. *
  19. * NB: Alot of this code was originally part of OpenIMSCore,
  20. * FhG Fokus.
  21. * Copyright (C) 2004-2006 FhG Fokus
  22. * Thanks for great work! This is an effort to
  23. * break apart the various CSCF functions into logically separate
  24. * components. We hope this will drive wider use. We also feel
  25. * that in this way the architecture is more complete and thereby easier
  26. * to manage in the Kamailio/SR environment
  27. *
  28. * This file is part of Kamailio, a free SIP server.
  29. *
  30. * Kamailio is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version
  34. *
  35. * Kamailio is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, write to the Free Software
  42. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  43. *
  44. */
  45. #ifndef AUTHORIZE_H
  46. #define AUTHORIZE_H
  47. #include "../../parser/msg_parser.h"
  48. #include "api.h"
  49. #include "conversion.h"
  50. #include "rfc2617.h"
  51. #include "sip_messages.h"
  52. #include "cxdx_mar.h"
  53. #define NONCE_LEN 16
  54. #define RAND_LEN 16
  55. enum authorization_types {
  56. AUTH_UNKNOWN = 0,
  57. /* 3GPP */
  58. AUTH_AKAV1_MD5 = 1,
  59. AUTH_AKAV2_MD5 = 2,
  60. AUTH_EARLY_IMS = 3,
  61. /* FOKUS */
  62. AUTH_MD5 = 4,
  63. /* CableLabs */
  64. AUTH_DIGEST = 5,
  65. /* 3GPP */
  66. AUTH_SIP_DIGEST = 6,
  67. /* TISPAN */
  68. AUTH_HTTP_DIGEST_MD5 = 7,
  69. AUTH_NASS_BUNDLED = 8
  70. };
  71. /** Enumeration for the Authorization Vector status */
  72. enum auth_vector_status {
  73. AUTH_VECTOR_UNUSED = 0,
  74. AUTH_VECTOR_SENT = 1,
  75. AUTH_VECTOR_USELESS = 2, /**< invalidated, marked for deletion */
  76. AUTH_VECTOR_USED = 3 /**< the vector has been successfully used */
  77. } ;
  78. /** Authorization Vector storage structure */
  79. typedef struct _auth_vector {
  80. int item_number; /**< index of the auth vector */
  81. unsigned char type; /**< type of authentication vector */
  82. str authenticate; /**< challenge (rand|autn in AKA) */
  83. str authorization; /**< expected response */
  84. str ck; /**< Cypher Key */
  85. str ik; /**< Integrity Key */
  86. time_t expires;/**< expires in (after it is sent) */
  87. uint32_t use_nb; /**< number of use (nonce count)*/
  88. enum auth_vector_status status;/**< current status */
  89. struct _auth_vector *next;/**< next av in the list */
  90. struct _auth_vector *prev;/**< previous av in the list */
  91. } auth_vector;
  92. /** Set of auth_vectors used by a private id */
  93. typedef struct _auth_userdata{
  94. unsigned int hash; /**< hash of the auth data */
  95. str private_identity; /**< authorization username */
  96. str public_identity; /**< public identity linked to */
  97. time_t expires; /**< expires in */
  98. auth_vector *head; /**< first auth vector in list */
  99. auth_vector *tail; /**< last auth vector in list */
  100. struct _auth_userdata *next;/**< next element in list */
  101. struct _auth_userdata *prev;/**< previous element in list*/
  102. } auth_userdata;
  103. /** Authorization user data hash slot */
  104. typedef struct {
  105. auth_userdata *head; /**< first in the slot */
  106. auth_userdata *tail; /**< last in the slot */
  107. gen_lock_t *lock; /**< slot lock */
  108. } auth_hash_slot_t;
  109. int auth_db_init(const str* db_url);
  110. int auth_db_bind(const str* db_url);
  111. void auth_db_close(void);
  112. /*
  113. * Authorize using Proxy-Authorization header field
  114. */
  115. int proxy_authenticate(struct sip_msg* _msg, char* _realm, char* _table);
  116. int proxy_challenge(struct sip_msg* msg, char* route, char* _realm, char* str2);
  117. /*
  118. * Authorize using WWW-Authorization header field
  119. */
  120. int www_authenticate(struct sip_msg* _msg, char* _realm, char* _table);
  121. int www_challenge2(struct sip_msg* msg, char* route, char* _realm, char* str2);
  122. int www_challenge3(struct sip_msg* msg, char* route, char* _realm, char* str2);
  123. int www_resync_auth(struct sip_msg* msg, char* _route, char* str1, char* str2);
  124. /*
  125. * Bind to IMS_AUTH API
  126. */
  127. int bind_ims_auth(ims_auth_api_t* api);
  128. auth_vector* get_auth_vector(str private_identity,str public_identity,int status,str *nonce,unsigned int *hash);
  129. /*
  130. * Storage of authentication vectors
  131. */
  132. inline void auth_data_lock(unsigned int hash);
  133. inline void auth_data_unlock(unsigned int hash);
  134. int auth_data_init(int size);
  135. void auth_data_destroy();
  136. auth_vector *new_auth_vector(int item_number,str auth_scheme,str authenticate,
  137. str authorization,str ck,str ik);
  138. void free_auth_vector(auth_vector *av);
  139. auth_userdata *new_auth_userdata(str private_identity,str public_identity);
  140. void free_auth_userdata(auth_userdata *aud);
  141. inline unsigned int get_hash_auth(str private_identity,str public_identity);
  142. int add_auth_vector(str private_identity,str public_identity,auth_vector *av);
  143. auth_vector* get_auth_vector(str private_identity,str public_identity,int status,str *nonce,unsigned int *hash);
  144. int drop_auth_userdata(str private_identity,str public_identity);
  145. auth_userdata* get_auth_userdata(str private_identity,str public_identity);
  146. int stateful_request_reply(struct sip_msg *msg, int code, char *text);
  147. int stateful_request_reply_async(struct cell* t, struct sip_msg *msg, int code, char *text);
  148. int multimedia_auth_request(struct sip_msg *msg, str public_identity, str private_identity,
  149. int count,str auth_scheme,str nonce,str auts,str servername, saved_transaction_t* transaction_data);
  150. int pack_challenge(struct sip_msg *msg,str realm,auth_vector *av, int is_proxy_auth);
  151. int add_authinfo_resp_hdr(struct sip_msg *msg, str nextnonce, str qop, HASHHEX rspauth, str cnonce, str nc);
  152. inline void start_reg_await_timer(auth_vector *av);
  153. void reg_await_timer(unsigned int ticks, void* param);
  154. unsigned char get_algorithm_type(str algorithm);
  155. #endif /* AUTHORIZE_H */