nonce.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * $Id$
  3. *
  4. * Nonce related functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #ifndef NONCE_H
  30. #define NONCE_H
  31. #include "../../parser/msg_parser.h"
  32. #include "../../parser/digest/digest.h"
  33. #include "../../str.h"
  34. #include "../../basex.h"
  35. #include <time.h>
  36. /* auth_extra_checks flags */
  37. #define AUTH_CHECK_FULL_URI (1 << 0)
  38. #define AUTH_CHECK_CALLID (1 << 1)
  39. #define AUTH_CHECK_FROMTAG (1 << 2)
  40. #define AUTH_CHECK_SRC_IP (1 << 3)
  41. /* nonce format:
  42. * base64(bin_nonce)
  43. * bin_nonce = expire_timestamp(4) | since_timestamp(4) | \
  44. * MD5(expire | since | secret1) (16) \
  45. * [| MD5(info(auth_extra_checks) | secret2) (16) ]
  46. * if nonce-count or one-time nonces are enabled, the format changes to:
  47. * bin_nonce =
  48. * bin_nonce = expire_timestamp(4) | since_timestamp(4) |
  49. * MD5(expire | since | nid | pf | secret1) [ | MD5... ] | nid(4) | pf(1)
  50. * where pf is 1 byte, first 2 bits are flags, and the other 6 are
  51. * the pool no:
  52. * bit7 : on => nid & pool are valid for nonce-count
  53. * bit6 : on => nid & pool are valid for one-time nonce
  54. */
  55. #if defined USE_NC || defined USE_OT_NONCE
  56. #define NF_VALID_NC_ID 128
  57. #define NF_VALID_OT_ID 64
  58. #define NF_POOL_NO_MASK 63
  59. #endif
  60. #if defined USE_NC || defined USE_OT_NONCE
  61. #define nonce_nid_extra_size (sizeof(unsigned int)+sizeof(unsigned char))
  62. #else /* USE_NC || USE_OT_NONCE*/
  63. #define nonce_nid_extra_size 0
  64. #endif /* USE_NC || USE_OT_NONCE */
  65. /* nonce structure, complete (maximum size) */
  66. struct bin_nonce_str{
  67. int expire;
  68. int since;
  69. char md5_1[16];
  70. char md5_2[16]; /* optional */
  71. #if defined USE_NC || defined USE_OT_NONCE
  72. unsigned int nid_i;
  73. unsigned char nid_pf; /* pool no & flags:
  74. bits 7, 6 = flags, bits 5..0 pool no*/
  75. #endif /* USE_NC || USE_OT_NONCE */
  76. };
  77. /* nonce structure, small version (no auth_extra_checks secondary md5) */
  78. struct bin_nonce_small_str{
  79. int expire;
  80. int since;
  81. char md5_1[16];
  82. #if defined USE_NC || defined USE_OT_NONCE
  83. unsigned int nid_i;
  84. unsigned char nid_pf; /* pool no & flags:
  85. bits 7, 6 = flags, bits 5..0 pool no*/
  86. #endif /* USE_NC || USE_OT_NONCE */
  87. };
  88. /* nonce union */
  89. union bin_nonce{
  90. struct bin_nonce_str n;
  91. struct bin_nonce_small_str n_small;
  92. unsigned char raw[sizeof(struct bin_nonce_str)];
  93. };
  94. /* fill an union bin_nonce*, before computing the md5 */
  95. #define BIN_NONCE_PREPARE_COMMON(bn, expire_val, since_val) \
  96. do{\
  97. (bn)->n.expire=htonl(expire_val); \
  98. (bn)->n.since=htonl(since_val); \
  99. }while(0)
  100. #if defined USE_NC || defined USE_OT_NONCE
  101. #define BIN_NONCE_PREPARE(bn, expire_v, since_v, id_v, pf_v, cfg, msg) \
  102. do{ \
  103. BIN_NONCE_PREPARE_COMMON(bn, expire_v, since_v); \
  104. if (cfg && msg){ \
  105. (bn)->n.nid_i=htonl(id_v); \
  106. (bn)->n.nid_pf=(pf_v); \
  107. }else{ \
  108. (bn)->n_small.nid_i=htonl(id_v); \
  109. (bn)->n_small.nid_pf=(pf_v); \
  110. } \
  111. }while(0)
  112. #else /* USE_NC || USE_OT_NONCE */
  113. #define BIN_NONCE_PREPARE(bn, expire, since, id, pf, cfg, msg) \
  114. BIN_NONCE_PREPARE_COMMON(bn, expire, since)
  115. #endif /* USE_NC || USE_OT_NONCE */
  116. /* maximum nonce length in binary form (not converted to base64/hex):
  117. * expires_t | since_t | MD5(expires_t | since_t | s1) | \
  118. * MD5(info(auth_extra_checks, s2)) => 4 + 4 + 16 + 16 = 40 bytes
  119. * or if nc_enabled:
  120. * expires_t | since_t | MD5...| MD5... | nonce_id | flag+pool_no(1 byte)
  121. * => 4 + 4 + 16 + 16 + 4 + 1 = 45 bytes
  122. * (sizeof(struct) cannot be used safely since structs can be padded
  123. * by the compiler if not defined with special attrs)
  124. */
  125. #if defined USE_NC || defined USE_OT_NONCE
  126. #define MAX_BIN_NONCE_LEN (4 + 4 + 16 + 16 + 4 +1)
  127. #define MAX_NOCFG_BIN_NONCE_LEN (4 + 4 + 16 + 4 + 1)
  128. #define get_bin_nonce_len(cfg, nid_enabled) \
  129. ( ( (cfg)?MAX_BIN_NONCE_LEN:MAX_NOCFG_BIN_NONCE_LEN ) - \
  130. (!(nid_enabled))*nonce_nid_extra_size )
  131. #else /* USE_NC || USE_OT_NONCE */
  132. #define MAX_BIN_NONCE_LEN (4 + 4 + 16 + 16)
  133. #define MAX_NOCFG_BIN_NONCE_LEN (4 + 4 + 16)
  134. #define get_bin_nonce_len(cfg, nid_enabled) \
  135. ( (cfg)?MAX_BIN_NONCE_LEN:MAX_NOCFG_BIN_NONCE_LEN )
  136. #endif /* USE_NC || USE_OT_NONCE */
  137. /* minimum nonce length in binary form (not converted to base64/hex):
  138. * expires_t | since_t | MD5(expires_t | since_t | s1) => 4 + 4 + 16 = 24
  139. * If nc_enabled the nonce will be bigger:
  140. * expires_t | since_t | MD5... | nonce_id | flag+pool_no(1 byte)
  141. * => 4 + 4 + 16 + 4 + 1 = 29, but we always return the minimum */
  142. #define MIN_BIN_NONCE_LEN (4 + 4 + 16)
  143. /*
  144. * Maximum length of nonce string in bytes
  145. * nonce = expires_TIMESTAMP[4 chars] since_TIMESTAMP[4 chars] \
  146. * MD5SUM(expires_TIMESTAMP, since_TIMESTAMP, SECRET1)[16 chars] \
  147. * MD5SUM(info(auth_extra_checks), SECRET2)[16 chars] \
  148. * [nid [4 chars] pflags[1 char]]
  149. */
  150. #define MAX_NONCE_LEN base64_enc_len(MAX_BIN_NONCE_LEN)
  151. /*
  152. * Minimum length of the nonce string
  153. * nonce = expires_TIMESTAMP[4 chars] since_TIMESTAMP[4 chars]
  154. * MD5SUM(expires_TIMESTAMP, since_TIMESTAMP, SECRET1)[16 chars]
  155. */
  156. #define MIN_NONCE_LEN base64_enc_len(MIN_BIN_NONCE_LEN)
  157. /*
  158. * length of nonces when no auth extra checks (cfg==0) are enabled
  159. */
  160. #define MAX_NOCFG_NONCE_LEN base64_enc_len(MAX_NOCFG_BIN_NONCE_LEN)
  161. /* Extra authentication checks for REGISTER messages */
  162. extern int auth_checks_reg;
  163. /* Extra authentication checks for out-of-dialog requests */
  164. extern int auth_checks_ood;
  165. /* Extra authentication checks for in-dialog requests */
  166. extern int auth_checks_ind;
  167. /* maximum time drift accepted for the nonce creation time
  168. * (e.g. nonce generated by another proxy in the same cluster with the
  169. * clock slightly in the future)
  170. */
  171. extern unsigned int nonce_auth_max_drift;
  172. int get_auth_checks(struct sip_msg* msg);
  173. /*
  174. * get the configured nonce len
  175. */
  176. #define get_nonce_len(cfg, nid_enabled) \
  177. base64_enc_len(get_bin_nonce_len(cfg, nid_enabled))
  178. /*
  179. * Calculate nonce value
  180. */
  181. int calc_nonce(char* nonce, int* nonce_len, int cfg, int since, int expires,
  182. #if defined USE_NC || defined USE_OT_NONCE
  183. unsigned int n_id, unsigned char pf,
  184. #endif /* USE_NC || USE_OT_NONCE */
  185. str* secret1, str* secret2, struct sip_msg* msg);
  186. /*
  187. * Check nonce value received from UA
  188. */
  189. int check_nonce(auth_body_t* auth, str* secret1, str* secret2,
  190. struct sip_msg* msg);
  191. #endif /* NONCE_H */