00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "httpauth.h"
00023 #include "libavutil/base64.h"
00024 #include "libavutil/avstring.h"
00025 #include "internal.h"
00026 #include "libavutil/random_seed.h"
00027 #include "libavutil/md5.h"
00028 #include "avformat.h"
00029 #include <ctype.h>
00030
00031 static void handle_basic_params(HTTPAuthState *state, const char *key,
00032 int key_len, char **dest, int *dest_len)
00033 {
00034 if (!strncmp(key, "realm=", key_len)) {
00035 *dest = state->realm;
00036 *dest_len = sizeof(state->realm);
00037 }
00038 }
00039
00040 static void handle_digest_params(HTTPAuthState *state, const char *key,
00041 int key_len, char **dest, int *dest_len)
00042 {
00043 DigestParams *digest = &state->digest_params;
00044
00045 if (!strncmp(key, "realm=", key_len)) {
00046 *dest = state->realm;
00047 *dest_len = sizeof(state->realm);
00048 } else if (!strncmp(key, "nonce=", key_len)) {
00049 *dest = digest->nonce;
00050 *dest_len = sizeof(digest->nonce);
00051 } else if (!strncmp(key, "opaque=", key_len)) {
00052 *dest = digest->opaque;
00053 *dest_len = sizeof(digest->opaque);
00054 } else if (!strncmp(key, "algorithm=", key_len)) {
00055 *dest = digest->algorithm;
00056 *dest_len = sizeof(digest->algorithm);
00057 } else if (!strncmp(key, "qop=", key_len)) {
00058 *dest = digest->qop;
00059 *dest_len = sizeof(digest->qop);
00060 } else if (!strncmp(key, "stale=", key_len)) {
00061 *dest = digest->stale;
00062 *dest_len = sizeof(digest->stale);
00063 }
00064 }
00065
00066 static void handle_digest_update(HTTPAuthState *state, const char *key,
00067 int key_len, char **dest, int *dest_len)
00068 {
00069 DigestParams *digest = &state->digest_params;
00070
00071 if (!strncmp(key, "nextnonce=", key_len)) {
00072 *dest = digest->nonce;
00073 *dest_len = sizeof(digest->nonce);
00074 }
00075 }
00076
00077 static void choose_qop(char *qop, int size)
00078 {
00079 char *ptr = strstr(qop, "auth");
00080 char *end = ptr + strlen("auth");
00081
00082 if (ptr && (!*end || isspace(*end) || *end == ',') &&
00083 (ptr == qop || isspace(ptr[-1]) || ptr[-1] == ',')) {
00084 av_strlcpy(qop, "auth", size);
00085 } else {
00086 qop[0] = 0;
00087 }
00088 }
00089
00090 void ff_http_auth_handle_header(HTTPAuthState *state, const char *key,
00091 const char *value)
00092 {
00093 if (!strcmp(key, "WWW-Authenticate") || !strcmp(key, "Proxy-Authenticate")) {
00094 const char *p;
00095 if (av_stristart(value, "Basic ", &p) &&
00096 state->auth_type <= HTTP_AUTH_BASIC) {
00097 state->auth_type = HTTP_AUTH_BASIC;
00098 state->realm[0] = 0;
00099 state->stale = 0;
00100 ff_parse_key_value(p, (ff_parse_key_val_cb) handle_basic_params,
00101 state);
00102 } else if (av_stristart(value, "Digest ", &p) &&
00103 state->auth_type <= HTTP_AUTH_DIGEST) {
00104 state->auth_type = HTTP_AUTH_DIGEST;
00105 memset(&state->digest_params, 0, sizeof(DigestParams));
00106 state->realm[0] = 0;
00107 state->stale = 0;
00108 ff_parse_key_value(p, (ff_parse_key_val_cb) handle_digest_params,
00109 state);
00110 choose_qop(state->digest_params.qop,
00111 sizeof(state->digest_params.qop));
00112 if (!av_strcasecmp(state->digest_params.stale, "true"))
00113 state->stale = 1;
00114 }
00115 } else if (!strcmp(key, "Authentication-Info")) {
00116 ff_parse_key_value(value, (ff_parse_key_val_cb) handle_digest_update,
00117 state);
00118 }
00119 }
00120
00121
00122 static void update_md5_strings(struct AVMD5 *md5ctx, ...)
00123 {
00124 va_list vl;
00125
00126 va_start(vl, md5ctx);
00127 while (1) {
00128 const char* str = va_arg(vl, const char*);
00129 if (!str)
00130 break;
00131 av_md5_update(md5ctx, str, strlen(str));
00132 }
00133 va_end(vl);
00134 }
00135
00136
00137 static char *make_digest_auth(HTTPAuthState *state, const char *username,
00138 const char *password, const char *uri,
00139 const char *method)
00140 {
00141 DigestParams *digest = &state->digest_params;
00142 int len;
00143 uint32_t cnonce_buf[2];
00144 char cnonce[17];
00145 char nc[9];
00146 int i;
00147 char A1hash[33], A2hash[33], response[33];
00148 struct AVMD5 *md5ctx;
00149 uint8_t hash[16];
00150 char *authstr;
00151
00152 digest->nc++;
00153 snprintf(nc, sizeof(nc), "%08x", digest->nc);
00154
00155
00156 for (i = 0; i < 2; i++)
00157 cnonce_buf[i] = av_get_random_seed();
00158 ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1);
00159 cnonce[2*sizeof(cnonce_buf)] = 0;
00160
00161 md5ctx = av_malloc(av_md5_size);
00162 if (!md5ctx)
00163 return NULL;
00164
00165 av_md5_init(md5ctx);
00166 update_md5_strings(md5ctx, username, ":", state->realm, ":", password, NULL);
00167 av_md5_final(md5ctx, hash);
00168 ff_data_to_hex(A1hash, hash, 16, 1);
00169 A1hash[32] = 0;
00170
00171 if (!strcmp(digest->algorithm, "") || !strcmp(digest->algorithm, "MD5")) {
00172 } else if (!strcmp(digest->algorithm, "MD5-sess")) {
00173 av_md5_init(md5ctx);
00174 update_md5_strings(md5ctx, A1hash, ":", digest->nonce, ":", cnonce, NULL);
00175 av_md5_final(md5ctx, hash);
00176 ff_data_to_hex(A1hash, hash, 16, 1);
00177 A1hash[32] = 0;
00178 } else {
00179
00180 av_free(md5ctx);
00181 return NULL;
00182 }
00183
00184 av_md5_init(md5ctx);
00185 update_md5_strings(md5ctx, method, ":", uri, NULL);
00186 av_md5_final(md5ctx, hash);
00187 ff_data_to_hex(A2hash, hash, 16, 1);
00188 A2hash[32] = 0;
00189
00190 av_md5_init(md5ctx);
00191 update_md5_strings(md5ctx, A1hash, ":", digest->nonce, NULL);
00192 if (!strcmp(digest->qop, "auth") || !strcmp(digest->qop, "auth-int")) {
00193 update_md5_strings(md5ctx, ":", nc, ":", cnonce, ":", digest->qop, NULL);
00194 }
00195 update_md5_strings(md5ctx, ":", A2hash, NULL);
00196 av_md5_final(md5ctx, hash);
00197 ff_data_to_hex(response, hash, 16, 1);
00198 response[32] = 0;
00199
00200 av_free(md5ctx);
00201
00202 if (!strcmp(digest->qop, "") || !strcmp(digest->qop, "auth")) {
00203 } else if (!strcmp(digest->qop, "auth-int")) {
00204
00205 return NULL;
00206 } else {
00207
00208 return NULL;
00209 }
00210
00211 len = strlen(username) + strlen(state->realm) + strlen(digest->nonce) +
00212 strlen(uri) + strlen(response) + strlen(digest->algorithm) +
00213 strlen(digest->opaque) + strlen(digest->qop) + strlen(cnonce) +
00214 strlen(nc) + 150;
00215
00216 authstr = av_malloc(len);
00217 if (!authstr)
00218 return NULL;
00219 snprintf(authstr, len, "Authorization: Digest ");
00220
00221
00222 av_strlcatf(authstr, len, "username=\"%s\"", username);
00223 av_strlcatf(authstr, len, ",realm=\"%s\"", state->realm);
00224 av_strlcatf(authstr, len, ",nonce=\"%s\"", digest->nonce);
00225 av_strlcatf(authstr, len, ",uri=\"%s\"", uri);
00226 av_strlcatf(authstr, len, ",response=\"%s\"", response);
00227 if (digest->algorithm[0])
00228 av_strlcatf(authstr, len, ",algorithm=%s", digest->algorithm);
00229 if (digest->opaque[0])
00230 av_strlcatf(authstr, len, ",opaque=\"%s\"", digest->opaque);
00231 if (digest->qop[0]) {
00232 av_strlcatf(authstr, len, ",qop=\"%s\"", digest->qop);
00233 av_strlcatf(authstr, len, ",cnonce=\"%s\"", cnonce);
00234 av_strlcatf(authstr, len, ",nc=%s", nc);
00235 }
00236
00237 av_strlcatf(authstr, len, "\r\n");
00238
00239 return authstr;
00240 }
00241
00242 char *ff_http_auth_create_response(HTTPAuthState *state, const char *auth,
00243 const char *path, const char *method)
00244 {
00245 char *authstr = NULL;
00246
00247
00248
00249 state->stale = 0;
00250 if (!auth || !strchr(auth, ':'))
00251 return NULL;
00252
00253 if (state->auth_type == HTTP_AUTH_BASIC) {
00254 int auth_b64_len = AV_BASE64_SIZE(strlen(auth));
00255 int len = auth_b64_len + 30;
00256 char *ptr;
00257 authstr = av_malloc(len);
00258 if (!authstr)
00259 return NULL;
00260 snprintf(authstr, len, "Authorization: Basic ");
00261 ptr = authstr + strlen(authstr);
00262 av_base64_encode(ptr, auth_b64_len, auth, strlen(auth));
00263 av_strlcat(ptr, "\r\n", len - (ptr - authstr));
00264 } else if (state->auth_type == HTTP_AUTH_DIGEST) {
00265 char *username = av_strdup(auth), *password;
00266
00267 if (!username)
00268 return NULL;
00269
00270 if ((password = strchr(username, ':'))) {
00271 *password++ = 0;
00272 authstr = make_digest_auth(state, username, password, path, method);
00273 }
00274 av_free(username);
00275 }
00276 return authstr;
00277 }