You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.2KB

  1. /*
  2. S->C:
  3. RTSP/1.0 401 Unauthorized
  4. CSeq: 302
  5. Date: 23 Jan 1997 15:35:06 GMT
  6. WWW-Authenticate: Digest
  7. realm="http-auth@example.org",
  8. qop="auth, auth-int",
  9. algorithm=SHA-256,
  10. nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
  11. opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"
  12. WWW-Authenticate: Digest
  13. realm="http-auth@example.org",
  14. qop="auth, auth-int",
  15. algorithm=MD5,
  16. nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
  17. opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"
  18. C->S:
  19. DESCRIBE rtsp://server.example.com/fizzle/foo RTSP/1.0
  20. CSeq: 312
  21. Accept: application/sdp, application/rtsl, application/mheg
  22. Authorization: Digest username="Mufasa",
  23. realm="http-auth@example.org",
  24. uri="/dir/index.html",
  25. algorithm=MD5,
  26. nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
  27. nc=00000001,
  28. cnonce="f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ",
  29. qop=auth,
  30. response="8ca523f5e9506fed4657c9700eebdbec",
  31. opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"
  32. */
  33. #include "rtsp-client-internal.h"
  34. int rtsp_client_authenrization(struct rtsp_client_t* rtsp, const char* method, const char* uri, const char* content, int length, char* authenrization, int bytes)
  35. {
  36. int n;
  37. if (0 != rtsp->auth.scheme)
  38. {
  39. rtsp->auth.nc += 1;
  40. snprintf(rtsp->auth.uri, sizeof(rtsp->auth.uri) - 1, "%s", uri);
  41. //snprintf(rtsp->auth.cnonce, sizeof(rtsp->auth.cnonce), "%p", rtsp); // TODO
  42. n = snprintf(authenrization, bytes, "Authorization: ");
  43. n += http_header_auth(&rtsp->auth, rtsp->pwd, method, content, length, authenrization + n, bytes - n);
  44. n += snprintf(authenrization + n, n < bytes ? bytes - n : 0, "\r\n");
  45. if (n > 0 && n < bytes)
  46. return n;
  47. }
  48. rtsp->authenrization[0] = 0;
  49. return 0;
  50. }
  51. int rtsp_client_www_authenticate(struct rtsp_client_t* rtsp, const char* filed)
  52. {
  53. memset(&rtsp->auth, 0, sizeof(rtsp->auth));
  54. snprintf(rtsp->auth.username, sizeof(rtsp->auth.username) - 1, "%s", rtsp->usr);
  55. if (0 != http_header_www_authenticate(filed, &rtsp->auth))
  56. {
  57. assert(0);
  58. return -1;
  59. }
  60. if (HTTP_AUTHENTICATION_BASIC != rtsp->auth.scheme && HTTP_AUTHENTICATION_DIGEST != rtsp->auth.scheme)
  61. {
  62. // only Basic/Digest support
  63. assert(0);
  64. return -1;
  65. }
  66. if (HTTP_AUTHENTICATION_BASIC != rtsp->auth.scheme && 0 != strcasecmp(rtsp->auth.algorithm, "MD5") && 0 != rtsp->auth.algorithm[0])
  67. {
  68. // only MD5 Digest support
  69. assert(0);
  70. return -1;
  71. }
  72. // support auth/auth-int only
  73. if (0 == memcmp(rtsp->auth.qop, "auth", 4))
  74. {
  75. rtsp->auth.qop[4] = 0;
  76. }
  77. else
  78. {
  79. // compatibility RFC2617
  80. rtsp->auth.qop[0] = 0;
  81. }
  82. return 0;
  83. }
  84. #if defined(_DEBUG) || defined(DEBUG)
  85. void rtsp_client_auth_test()
  86. {
  87. struct rtsp_client_t rtsp;
  88. memset(&rtsp, 0, sizeof(rtsp));
  89. strcpy(rtsp.pwd, "Circle Of Life");
  90. rtsp.auth.scheme = HTTP_AUTHENTICATION_DIGEST;
  91. strcpy(rtsp.auth.username, "Mufasa");
  92. strcpy(rtsp.auth.realm, "testrealm@host.com");
  93. strcpy(rtsp.auth.nonce, "dcd98b7102dd2f0e8b11d0f600bfb0c093");
  94. strcpy(rtsp.auth.opaque, "5ccc069c403ebaf9f0171e9517f40e41");
  95. strcpy(rtsp.auth.cnonce, "0a4f113b");
  96. strcpy(rtsp.auth.qop, "auth");
  97. rtsp_client_authenrization(&rtsp, "GET", "/dir/index.html", NULL, 0, rtsp.req, sizeof(rtsp.req));
  98. assert(strstr(rtsp.req, "response=\"6629fae49393a05397450978507c4ef1\""));
  99. }
  100. #endif