httpauth.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * HTTP authentication
  3. * Copyright (c) 2010 Martin Storsjo
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "httpauth.h"
  22. #include "libavutil/base64.h"
  23. #include "libavutil/avstring.h"
  24. #include "internal.h"
  25. #include "libavutil/random_seed.h"
  26. #include "libavutil/md5.h"
  27. #include "urldecode.h"
  28. #include "avformat.h"
  29. #include <ctype.h>
  30. static void handle_basic_params(HTTPAuthState *state, const char *key,
  31. int key_len, char **dest, int *dest_len)
  32. {
  33. if (!strncmp(key, "realm=", key_len)) {
  34. *dest = state->realm;
  35. *dest_len = sizeof(state->realm);
  36. }
  37. }
  38. static void handle_digest_params(HTTPAuthState *state, const char *key,
  39. int key_len, char **dest, int *dest_len)
  40. {
  41. DigestParams *digest = &state->digest_params;
  42. if (!strncmp(key, "realm=", key_len)) {
  43. *dest = state->realm;
  44. *dest_len = sizeof(state->realm);
  45. } else if (!strncmp(key, "nonce=", key_len)) {
  46. *dest = digest->nonce;
  47. *dest_len = sizeof(digest->nonce);
  48. } else if (!strncmp(key, "opaque=", key_len)) {
  49. *dest = digest->opaque;
  50. *dest_len = sizeof(digest->opaque);
  51. } else if (!strncmp(key, "algorithm=", key_len)) {
  52. *dest = digest->algorithm;
  53. *dest_len = sizeof(digest->algorithm);
  54. } else if (!strncmp(key, "qop=", key_len)) {
  55. *dest = digest->qop;
  56. *dest_len = sizeof(digest->qop);
  57. } else if (!strncmp(key, "stale=", key_len)) {
  58. *dest = digest->stale;
  59. *dest_len = sizeof(digest->stale);
  60. }
  61. }
  62. static void handle_digest_update(HTTPAuthState *state, const char *key,
  63. int key_len, char **dest, int *dest_len)
  64. {
  65. DigestParams *digest = &state->digest_params;
  66. if (!strncmp(key, "nextnonce=", key_len)) {
  67. *dest = digest->nonce;
  68. *dest_len = sizeof(digest->nonce);
  69. }
  70. }
  71. static void choose_qop(char *qop, int size)
  72. {
  73. char *ptr = strstr(qop, "auth");
  74. char *end = ptr + strlen("auth");
  75. if (ptr && (!*end || isspace(*end) || *end == ',') &&
  76. (ptr == qop || isspace(ptr[-1]) || ptr[-1] == ',')) {
  77. av_strlcpy(qop, "auth", size);
  78. } else {
  79. qop[0] = 0;
  80. }
  81. }
  82. void ff_http_auth_handle_header(HTTPAuthState *state, const char *key,
  83. const char *value)
  84. {
  85. if (!strcmp(key, "WWW-Authenticate") || !strcmp(key, "Proxy-Authenticate")) {
  86. const char *p;
  87. if (av_stristart(value, "Basic ", &p) &&
  88. state->auth_type <= HTTP_AUTH_BASIC) {
  89. state->auth_type = HTTP_AUTH_BASIC;
  90. state->realm[0] = 0;
  91. state->stale = 0;
  92. ff_parse_key_value(p, (ff_parse_key_val_cb) handle_basic_params,
  93. state);
  94. } else if (av_stristart(value, "Digest ", &p) &&
  95. state->auth_type <= HTTP_AUTH_DIGEST) {
  96. state->auth_type = HTTP_AUTH_DIGEST;
  97. memset(&state->digest_params, 0, sizeof(DigestParams));
  98. state->realm[0] = 0;
  99. state->stale = 0;
  100. ff_parse_key_value(p, (ff_parse_key_val_cb) handle_digest_params,
  101. state);
  102. choose_qop(state->digest_params.qop,
  103. sizeof(state->digest_params.qop));
  104. if (!av_strcasecmp(state->digest_params.stale, "true"))
  105. state->stale = 1;
  106. }
  107. } else if (!strcmp(key, "Authentication-Info")) {
  108. ff_parse_key_value(value, (ff_parse_key_val_cb) handle_digest_update,
  109. state);
  110. }
  111. }
  112. static void update_md5_strings(struct AVMD5 *md5ctx, ...)
  113. {
  114. va_list vl;
  115. va_start(vl, md5ctx);
  116. while (1) {
  117. const char* str = va_arg(vl, const char*);
  118. if (!str)
  119. break;
  120. av_md5_update(md5ctx, str, strlen(str));
  121. }
  122. va_end(vl);
  123. }
  124. /* Generate a digest reply, according to RFC 2617. */
  125. static char *make_digest_auth(HTTPAuthState *state, const char *username,
  126. const char *password, const char *uri,
  127. const char *method)
  128. {
  129. DigestParams *digest = &state->digest_params;
  130. int len;
  131. uint32_t cnonce_buf[2];
  132. char cnonce[17];
  133. char nc[9];
  134. int i;
  135. char A1hash[33], A2hash[33], response[33];
  136. struct AVMD5 *md5ctx;
  137. uint8_t hash[16];
  138. char *authstr;
  139. digest->nc++;
  140. snprintf(nc, sizeof(nc), "%08x", digest->nc);
  141. /* Generate a client nonce. */
  142. for (i = 0; i < 2; i++)
  143. cnonce_buf[i] = av_get_random_seed();
  144. ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1);
  145. cnonce[2*sizeof(cnonce_buf)] = 0;
  146. md5ctx = av_malloc(av_md5_size);
  147. if (!md5ctx)
  148. return NULL;
  149. av_md5_init(md5ctx);
  150. update_md5_strings(md5ctx, username, ":", state->realm, ":", password, NULL);
  151. av_md5_final(md5ctx, hash);
  152. ff_data_to_hex(A1hash, hash, 16, 1);
  153. A1hash[32] = 0;
  154. if (!strcmp(digest->algorithm, "") || !strcmp(digest->algorithm, "MD5")) {
  155. } else if (!strcmp(digest->algorithm, "MD5-sess")) {
  156. av_md5_init(md5ctx);
  157. update_md5_strings(md5ctx, A1hash, ":", digest->nonce, ":", cnonce, NULL);
  158. av_md5_final(md5ctx, hash);
  159. ff_data_to_hex(A1hash, hash, 16, 1);
  160. A1hash[32] = 0;
  161. } else {
  162. /* Unsupported algorithm */
  163. av_free(md5ctx);
  164. return NULL;
  165. }
  166. av_md5_init(md5ctx);
  167. update_md5_strings(md5ctx, method, ":", uri, NULL);
  168. av_md5_final(md5ctx, hash);
  169. ff_data_to_hex(A2hash, hash, 16, 1);
  170. A2hash[32] = 0;
  171. av_md5_init(md5ctx);
  172. update_md5_strings(md5ctx, A1hash, ":", digest->nonce, NULL);
  173. if (!strcmp(digest->qop, "auth") || !strcmp(digest->qop, "auth-int")) {
  174. update_md5_strings(md5ctx, ":", nc, ":", cnonce, ":", digest->qop, NULL);
  175. }
  176. update_md5_strings(md5ctx, ":", A2hash, NULL);
  177. av_md5_final(md5ctx, hash);
  178. ff_data_to_hex(response, hash, 16, 1);
  179. response[32] = 0;
  180. av_free(md5ctx);
  181. if (!strcmp(digest->qop, "") || !strcmp(digest->qop, "auth")) {
  182. } else if (!strcmp(digest->qop, "auth-int")) {
  183. /* qop=auth-int not supported */
  184. return NULL;
  185. } else {
  186. /* Unsupported qop value. */
  187. return NULL;
  188. }
  189. len = strlen(username) + strlen(state->realm) + strlen(digest->nonce) +
  190. strlen(uri) + strlen(response) + strlen(digest->algorithm) +
  191. strlen(digest->opaque) + strlen(digest->qop) + strlen(cnonce) +
  192. strlen(nc) + 150;
  193. authstr = av_malloc(len);
  194. if (!authstr)
  195. return NULL;
  196. snprintf(authstr, len, "Authorization: Digest ");
  197. /* TODO: Escape the quoted strings properly. */
  198. av_strlcatf(authstr, len, "username=\"%s\"", username);
  199. av_strlcatf(authstr, len, ",realm=\"%s\"", state->realm);
  200. av_strlcatf(authstr, len, ",nonce=\"%s\"", digest->nonce);
  201. av_strlcatf(authstr, len, ",uri=\"%s\"", uri);
  202. av_strlcatf(authstr, len, ",response=\"%s\"", response);
  203. if (digest->algorithm[0])
  204. av_strlcatf(authstr, len, ",algorithm=%s", digest->algorithm);
  205. if (digest->opaque[0])
  206. av_strlcatf(authstr, len, ",opaque=\"%s\"", digest->opaque);
  207. if (digest->qop[0]) {
  208. av_strlcatf(authstr, len, ",qop=\"%s\"", digest->qop);
  209. av_strlcatf(authstr, len, ",cnonce=\"%s\"", cnonce);
  210. av_strlcatf(authstr, len, ",nc=%s", nc);
  211. }
  212. av_strlcatf(authstr, len, "\r\n");
  213. return authstr;
  214. }
  215. char *ff_http_auth_create_response(HTTPAuthState *state, const char *auth,
  216. const char *path, const char *method)
  217. {
  218. char *authstr = NULL;
  219. /* Clear the stale flag, we assume the auth is ok now. It is reset
  220. * by the server headers if there's a new issue. */
  221. state->stale = 0;
  222. if (!auth || !strchr(auth, ':'))
  223. return NULL;
  224. if (state->auth_type == HTTP_AUTH_BASIC) {
  225. int auth_b64_len, len;
  226. char *ptr, *decoded_auth = ff_urldecode(auth);
  227. if (!decoded_auth)
  228. return NULL;
  229. auth_b64_len = AV_BASE64_SIZE(strlen(decoded_auth));
  230. len = auth_b64_len + 30;
  231. authstr = av_malloc(len);
  232. if (!authstr) {
  233. av_free(decoded_auth);
  234. return NULL;
  235. }
  236. snprintf(authstr, len, "Authorization: Basic ");
  237. ptr = authstr + strlen(authstr);
  238. av_base64_encode(ptr, auth_b64_len, decoded_auth, strlen(decoded_auth));
  239. av_strlcat(ptr, "\r\n", len - (ptr - authstr));
  240. av_free(decoded_auth);
  241. } else if (state->auth_type == HTTP_AUTH_DIGEST) {
  242. char *username = ff_urldecode(auth), *password;
  243. if (!username)
  244. return NULL;
  245. if ((password = strchr(username, ':'))) {
  246. *password++ = 0;
  247. authstr = make_digest_auth(state, username, password, path, method);
  248. }
  249. av_free(username);
  250. }
  251. return authstr;
  252. }