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.

322 lines
10KB

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