rfc2617.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * $Id$
  3. *
  4. * Digest response calculation as per RFC2617
  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. #include <sys/types.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "rfc2617.h"
  33. #include "../../md5.h"
  34. #include "../../dprint.h"
  35. inline void cvt_hex(HASH _b, HASHHEX _h)
  36. {
  37. unsigned short i;
  38. unsigned char j;
  39. for (i = 0; i < HASHLEN; i++) {
  40. j = (_b[i] >> 4) & 0xf;
  41. if (j <= 9) {
  42. _h[i * 2] = (j + '0');
  43. } else {
  44. _h[i * 2] = (j + 'a' - 10);
  45. }
  46. j = _b[i] & 0xf;
  47. if (j <= 9) {
  48. _h[i * 2 + 1] = (j + '0');
  49. } else {
  50. _h[i * 2 + 1] = (j + 'a' - 10);
  51. }
  52. };
  53. _h[HASHHEXLEN] = '\0';
  54. }
  55. /*
  56. * calculate H(A1) as per spec
  57. */
  58. void calc_HA1(ha_alg_t _alg, str* _username, str* _realm, str* _password,
  59. str* _nonce, str* _cnonce, HASHHEX _sess_key)
  60. {
  61. MD5_CTX Md5Ctx;
  62. HASH HA1;
  63. MD5Init(&Md5Ctx);
  64. MD5Update(&Md5Ctx, _username->s, _username->len);
  65. MD5Update(&Md5Ctx, ":", 1);
  66. MD5Update(&Md5Ctx, _realm->s, _realm->len);
  67. MD5Update(&Md5Ctx, ":", 1);
  68. MD5Update(&Md5Ctx, _password->s, _password->len);
  69. MD5Final(HA1, &Md5Ctx);
  70. if (_alg == HA_MD5_SESS) {
  71. MD5Init(&Md5Ctx);
  72. MD5Update(&Md5Ctx, HA1, HASHLEN);
  73. MD5Update(&Md5Ctx, ":", 1);
  74. MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
  75. MD5Update(&Md5Ctx, ":", 1);
  76. MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
  77. MD5Final(HA1, &Md5Ctx);
  78. };
  79. cvt_hex(HA1, _sess_key);
  80. }
  81. /*
  82. * calculate request-digest/response-digest as per HTTP Digest spec
  83. */
  84. void calc_response(HASHHEX _ha1, /* H(A1) */
  85. str* _nonce, /* nonce from server */
  86. str* _nc, /* 8 hex digits */
  87. str* _cnonce, /* client nonce */
  88. str* _qop, /* qop-value: "", "auth", "auth-int" */
  89. int _auth_int, /* 1 if auth-int is used */
  90. str* _method, /* method from the request */
  91. str* _uri, /* requested URL */
  92. HASHHEX _hentity, /* H(entity body) if qop="auth-int" */
  93. HASHHEX _response) /* request-digest or response-digest */
  94. {
  95. MD5_CTX Md5Ctx;
  96. HASH HA2;
  97. HASH RespHash;
  98. HASHHEX HA2Hex;
  99. /* calculate H(A2) */
  100. MD5Init(&Md5Ctx);
  101. MD5Update(&Md5Ctx, _method->s, _method->len);
  102. MD5Update(&Md5Ctx, ":", 1);
  103. MD5Update(&Md5Ctx, _uri->s, _uri->len);
  104. if (_auth_int) {
  105. MD5Update(&Md5Ctx, ":", 1);
  106. MD5Update(&Md5Ctx, _hentity, HASHHEXLEN);
  107. };
  108. MD5Final(HA2, &Md5Ctx);
  109. cvt_hex(HA2, HA2Hex);
  110. /* calculate response */
  111. MD5Init(&Md5Ctx);
  112. MD5Update(&Md5Ctx, _ha1, HASHHEXLEN);
  113. MD5Update(&Md5Ctx, ":", 1);
  114. MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
  115. MD5Update(&Md5Ctx, ":", 1);
  116. if (_qop->len) {
  117. MD5Update(&Md5Ctx, _nc->s, _nc->len);
  118. MD5Update(&Md5Ctx, ":", 1);
  119. MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
  120. MD5Update(&Md5Ctx, ":", 1);
  121. MD5Update(&Md5Ctx, _qop->s, _qop->len);
  122. MD5Update(&Md5Ctx, ":", 1);
  123. };
  124. MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
  125. MD5Final(RespHash, &Md5Ctx);
  126. cvt_hex(RespHash, _response);
  127. }