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.

278 lines
9.3KB

  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 "avformat.h"
  28. #include <ctype.h>
  29. static void handle_basic_params(HTTPAuthState *state, const char *key,
  30. int key_len, char **dest, int *dest_len)
  31. {
  32. if (!strncmp(key, "realm=", key_len)) {
  33. *dest = state->realm;
  34. *dest_len = sizeof(state->realm);
  35. }
  36. }
  37. static void handle_digest_params(HTTPAuthState *state, const char *key,
  38. int key_len, char **dest, int *dest_len)
  39. {
  40. DigestParams *digest = &state->digest_params;
  41. if (!strncmp(key, "realm=", key_len)) {
  42. *dest = state->realm;
  43. *dest_len = sizeof(state->realm);
  44. } else if (!strncmp(key, "nonce=", key_len)) {
  45. *dest = digest->nonce;
  46. *dest_len = sizeof(digest->nonce);
  47. } else if (!strncmp(key, "opaque=", key_len)) {
  48. *dest = digest->opaque;
  49. *dest_len = sizeof(digest->opaque);
  50. } else if (!strncmp(key, "algorithm=", key_len)) {
  51. *dest = digest->algorithm;
  52. *dest_len = sizeof(digest->algorithm);
  53. } else if (!strncmp(key, "qop=", key_len)) {
  54. *dest = digest->qop;
  55. *dest_len = sizeof(digest->qop);
  56. } else if (!strncmp(key, "stale=", key_len)) {
  57. *dest = digest->stale;
  58. *dest_len = sizeof(digest->stale);
  59. }
  60. }
  61. static void handle_digest_update(HTTPAuthState *state, const char *key,
  62. int key_len, char **dest, int *dest_len)
  63. {
  64. DigestParams *digest = &state->digest_params;
  65. if (!strncmp(key, "nextnonce=", key_len)) {
  66. *dest = digest->nonce;
  67. *dest_len = sizeof(digest->nonce);
  68. }
  69. }
  70. static void choose_qop(char *qop, int size)
  71. {
  72. char *ptr = strstr(qop, "auth");
  73. char *end = ptr + strlen("auth");
  74. if (ptr && (!*end || isspace(*end) || *end == ',') &&
  75. (ptr == qop || isspace(ptr[-1]) || ptr[-1] == ',')) {
  76. av_strlcpy(qop, "auth", size);
  77. } else {
  78. qop[0] = 0;
  79. }
  80. }
  81. void ff_http_auth_handle_header(HTTPAuthState *state, const char *key,
  82. const char *value)
  83. {
  84. if (!strcmp(key, "WWW-Authenticate") || !strcmp(key, "Proxy-Authenticate")) {
  85. const char *p;
  86. if (av_stristart(value, "Basic ", &p) &&
  87. state->auth_type <= HTTP_AUTH_BASIC) {
  88. state->auth_type = HTTP_AUTH_BASIC;
  89. state->realm[0] = 0;
  90. state->stale = 0;
  91. ff_parse_key_value(p, (ff_parse_key_val_cb) handle_basic_params,
  92. state);
  93. } else if (av_stristart(value, "Digest ", &p) &&
  94. state->auth_type <= HTTP_AUTH_DIGEST) {
  95. state->auth_type = HTTP_AUTH_DIGEST;
  96. memset(&state->digest_params, 0, sizeof(DigestParams));
  97. state->realm[0] = 0;
  98. state->stale = 0;
  99. ff_parse_key_value(p, (ff_parse_key_val_cb) handle_digest_params,
  100. state);
  101. choose_qop(state->digest_params.qop,
  102. sizeof(state->digest_params.qop));
  103. if (!av_strcasecmp(state->digest_params.stale, "true"))
  104. state->stale = 1;
  105. }
  106. } else if (!strcmp(key, "Authentication-Info")) {
  107. ff_parse_key_value(value, (ff_parse_key_val_cb) handle_digest_update,
  108. state);
  109. }
  110. }
  111. static void update_md5_strings(struct AVMD5 *md5ctx, ...)
  112. {
  113. va_list vl;
  114. va_start(vl, md5ctx);
  115. while (1) {
  116. const char* str = va_arg(vl, const char*);
  117. if (!str)
  118. break;
  119. av_md5_update(md5ctx, str, strlen(str));
  120. }
  121. va_end(vl);
  122. }
  123. /* Generate a digest reply, according to RFC 2617. */
  124. static char *make_digest_auth(HTTPAuthState *state, const char *username,
  125. const char *password, const char *uri,
  126. const char *method)
  127. {
  128. DigestParams *digest = &state->digest_params;
  129. int len;
  130. uint32_t cnonce_buf[2];
  131. char cnonce[17];
  132. char nc[9];
  133. int i;
  134. char A1hash[33], A2hash[33], response[33];
  135. struct AVMD5 *md5ctx;
  136. uint8_t hash[16];
  137. char *authstr;
  138. digest->nc++;
  139. snprintf(nc, sizeof(nc), "%08x", digest->nc);
  140. /* Generate a client nonce. */
  141. for (i = 0; i < 2; i++)
  142. cnonce_buf[i] = av_get_random_seed();
  143. ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1);
  144. cnonce[2*sizeof(cnonce_buf)] = 0;
  145. md5ctx = av_malloc(av_md5_size);
  146. if (!md5ctx)
  147. return NULL;
  148. av_md5_init(md5ctx);
  149. update_md5_strings(md5ctx, username, ":", state->realm, ":", password, NULL);
  150. av_md5_final(md5ctx, hash);
  151. ff_data_to_hex(A1hash, hash, 16, 1);
  152. A1hash[32] = 0;
  153. if (!strcmp(digest->algorithm, "") || !strcmp(digest->algorithm, "MD5")) {
  154. } else if (!strcmp(digest->algorithm, "MD5-sess")) {
  155. av_md5_init(md5ctx);
  156. update_md5_strings(md5ctx, A1hash, ":", digest->nonce, ":", cnonce, NULL);
  157. av_md5_final(md5ctx, hash);
  158. ff_data_to_hex(A1hash, hash, 16, 1);
  159. A1hash[32] = 0;
  160. } else {
  161. /* Unsupported algorithm */
  162. av_free(md5ctx);
  163. return NULL;
  164. }
  165. av_md5_init(md5ctx);
  166. update_md5_strings(md5ctx, method, ":", uri, NULL);
  167. av_md5_final(md5ctx, hash);
  168. ff_data_to_hex(A2hash, hash, 16, 1);
  169. A2hash[32] = 0;
  170. av_md5_init(md5ctx);
  171. update_md5_strings(md5ctx, A1hash, ":", digest->nonce, NULL);
  172. if (!strcmp(digest->qop, "auth") || !strcmp(digest->qop, "auth-int")) {
  173. update_md5_strings(md5ctx, ":", nc, ":", cnonce, ":", digest->qop, NULL);
  174. }
  175. update_md5_strings(md5ctx, ":", A2hash, NULL);
  176. av_md5_final(md5ctx, hash);
  177. ff_data_to_hex(response, hash, 16, 1);
  178. response[32] = 0;
  179. av_free(md5ctx);
  180. if (!strcmp(digest->qop, "") || !strcmp(digest->qop, "auth")) {
  181. } else if (!strcmp(digest->qop, "auth-int")) {
  182. /* qop=auth-int not supported */
  183. return NULL;
  184. } else {
  185. /* Unsupported qop value. */
  186. return NULL;
  187. }
  188. len = strlen(username) + strlen(state->realm) + strlen(digest->nonce) +
  189. strlen(uri) + strlen(response) + strlen(digest->algorithm) +
  190. strlen(digest->opaque) + strlen(digest->qop) + strlen(cnonce) +
  191. strlen(nc) + 150;
  192. authstr = av_malloc(len);
  193. if (!authstr)
  194. return NULL;
  195. snprintf(authstr, len, "Authorization: Digest ");
  196. /* TODO: Escape the quoted strings properly. */
  197. av_strlcatf(authstr, len, "username=\"%s\"", username);
  198. av_strlcatf(authstr, len, ",realm=\"%s\"", state->realm);
  199. av_strlcatf(authstr, len, ",nonce=\"%s\"", digest->nonce);
  200. av_strlcatf(authstr, len, ",uri=\"%s\"", uri);
  201. av_strlcatf(authstr, len, ",response=\"%s\"", response);
  202. if (digest->algorithm[0])
  203. av_strlcatf(authstr, len, ",algorithm=%s", digest->algorithm);
  204. if (digest->opaque[0])
  205. av_strlcatf(authstr, len, ",opaque=\"%s\"", digest->opaque);
  206. if (digest->qop[0]) {
  207. av_strlcatf(authstr, len, ",qop=\"%s\"", digest->qop);
  208. av_strlcatf(authstr, len, ",cnonce=\"%s\"", cnonce);
  209. av_strlcatf(authstr, len, ",nc=%s", nc);
  210. }
  211. av_strlcatf(authstr, len, "\r\n");
  212. return authstr;
  213. }
  214. char *ff_http_auth_create_response(HTTPAuthState *state, const char *auth,
  215. const char *path, const char *method)
  216. {
  217. char *authstr = NULL;
  218. /* Clear the stale flag, we assume the auth is ok now. It is reset
  219. * by the server headers if there's a new issue. */
  220. state->stale = 0;
  221. if (!auth || !strchr(auth, ':'))
  222. return NULL;
  223. if (state->auth_type == HTTP_AUTH_BASIC) {
  224. int auth_b64_len = AV_BASE64_SIZE(strlen(auth));
  225. int len = auth_b64_len + 30;
  226. char *ptr;
  227. authstr = av_malloc(len);
  228. if (!authstr)
  229. return NULL;
  230. snprintf(authstr, len, "Authorization: Basic ");
  231. ptr = authstr + strlen(authstr);
  232. av_base64_encode(ptr, auth_b64_len, auth, strlen(auth));
  233. av_strlcat(ptr, "\r\n", len - (ptr - authstr));
  234. } else if (state->auth_type == HTTP_AUTH_DIGEST) {
  235. char *username = av_strdup(auth), *password;
  236. if (!username)
  237. return NULL;
  238. if ((password = strchr(username, ':'))) {
  239. *password++ = 0;
  240. authstr = make_digest_auth(state, username, password, path, method);
  241. }
  242. av_free(username);
  243. }
  244. return authstr;
  245. }