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