rfc2617.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <sys/types.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include "rfc2617.h"
  49. #include "../../md5.h"
  50. #include "../../dprint.h"
  51. inline void cvt_hex(HASH _b, HASHHEX _h)
  52. {
  53. unsigned short i;
  54. unsigned char j;
  55. for (i = 0; i < HASHLEN; i++) {
  56. j = (_b[i] >> 4) & 0xf;
  57. if (j <= 9) {
  58. _h[i * 2] = (j + '0');
  59. } else {
  60. _h[i * 2] = (j + 'a' - 10);
  61. }
  62. j = _b[i] & 0xf;
  63. if (j <= 9) {
  64. _h[i * 2 + 1] = (j + '0');
  65. } else {
  66. _h[i * 2 + 1] = (j + 'a' - 10);
  67. }
  68. };
  69. _h[HASHHEXLEN] = '\0';
  70. }
  71. /*
  72. * calculate H(A1) as per spec
  73. */
  74. void calc_HA1(ha_alg_t _alg, str* _username, str* _realm, str* _password,
  75. str* _nonce, str* _cnonce, HASHHEX _sess_key)
  76. {
  77. MD5_CTX Md5Ctx;
  78. HASH HA1;
  79. MD5Init(&Md5Ctx);
  80. MD5Update(&Md5Ctx, _username->s, _username->len);
  81. MD5Update(&Md5Ctx, ":", 1);
  82. MD5Update(&Md5Ctx, _realm->s, _realm->len);
  83. MD5Update(&Md5Ctx, ":", 1);
  84. MD5Update(&Md5Ctx, _password->s, _password->len);
  85. MD5Final(HA1, &Md5Ctx);
  86. if (_alg == HA_MD5_SESS) {
  87. MD5Init(&Md5Ctx);
  88. MD5Update(&Md5Ctx, HA1, HASHLEN);
  89. MD5Update(&Md5Ctx, ":", 1);
  90. MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
  91. MD5Update(&Md5Ctx, ":", 1);
  92. MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
  93. MD5Final(HA1, &Md5Ctx);
  94. };
  95. cvt_hex(HA1, _sess_key);
  96. }
  97. void calc_H(str *ent, HASHHEX hash)
  98. {
  99. MD5_CTX Md5Ctx;
  100. HASH HA1;
  101. MD5Init(&Md5Ctx);
  102. MD5Update(&Md5Ctx, ent->s, ent->len);
  103. MD5Final(HA1, &Md5Ctx);
  104. cvt_hex(HA1, hash);
  105. }
  106. /*
  107. * calculate request-digest/response-digest as per HTTP Digest spec
  108. */
  109. void calc_response(HASHHEX _ha1, /* H(A1) */
  110. str* _nonce, /* nonce from server */
  111. str* _nc, /* 8 hex digits */
  112. str* _cnonce, /* client nonce */
  113. str* _qop, /* qop-value: "", "auth", "auth-int" */
  114. int _auth_int, /* 1 if auth-int is used */
  115. str* _method, /* method from the request */
  116. str* _uri, /* requested URL */
  117. HASHHEX _hentity, /* H(entity body) if qop="auth-int" */
  118. HASHHEX _response) /* request-digest or response-digest */
  119. {
  120. LM_DBG("calc_response(_ha1=%.*s, _nonce=%.*s, _nc=%.*s,_cnonce=%.*s, _qop=%.*s, _auth_int=%d,_method=%.*s,_uri=%.*s,_hentity=%.*s)\n",
  121. HASHHEXLEN, _ha1,
  122. _nonce->len, _nonce->s,
  123. _nc->len, _nc->s,
  124. _cnonce->len, _cnonce->s,
  125. _qop->len, _qop->s,
  126. _auth_int,
  127. _method ? _method->len : 4, _method ? _method->s : "null",
  128. _uri->len, _uri->s,
  129. _auth_int ? HASHHEXLEN : 0, _hentity);
  130. MD5_CTX Md5Ctx;
  131. HASH HA2;
  132. HASH RespHash;
  133. HASHHEX HA2Hex;
  134. /* calculate H(A2) */
  135. MD5Init(&Md5Ctx);
  136. if (_method) { /* _method is NULL when calculating H(A2) for rspauth in Authentication-Info */
  137. MD5Update(&Md5Ctx, _method->s, _method->len);
  138. }
  139. MD5Update(&Md5Ctx, ":", 1);
  140. MD5Update(&Md5Ctx, _uri->s, _uri->len);
  141. if (_auth_int) {
  142. MD5Update(&Md5Ctx, ":", 1);
  143. MD5Update(&Md5Ctx, _hentity, HASHHEXLEN);
  144. };
  145. MD5Final(HA2, &Md5Ctx);
  146. cvt_hex(HA2, HA2Hex);
  147. /* calculate response */
  148. MD5Init(&Md5Ctx);
  149. MD5Update(&Md5Ctx, _ha1, HASHHEXLEN);
  150. MD5Update(&Md5Ctx, ":", 1);
  151. MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
  152. MD5Update(&Md5Ctx, ":", 1);
  153. if (_qop->len) {
  154. MD5Update(&Md5Ctx, _nc->s, _nc->len);
  155. MD5Update(&Md5Ctx, ":", 1);
  156. MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
  157. MD5Update(&Md5Ctx, ":", 1);
  158. MD5Update(&Md5Ctx, _qop->s, _qop->len);
  159. MD5Update(&Md5Ctx, ":", 1);
  160. };
  161. MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
  162. MD5Final(RespHash, &Md5Ctx);
  163. cvt_hex(RespHash, _response);
  164. LM_DBG("H(A1) = %.*s, H(A2) = %.*s, rspauth = %.*s\n",
  165. HASHHEXLEN, _ha1, HASHHEXLEN, HA2Hex, HASHHEXLEN, _response);
  166. }