annotate httpauth.c @ 6469:eb68008daac2 libavformat

Add a muxer and demuxer for raw G.722
author mstorsjo
date Thu, 09 Sep 2010 19:27:41 +0000
parents 054de75e4a49
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
25 #include "internal.h"
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
26 #include "libavutil/random_seed.h"
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
40 static void handle_digest_params(HTTPAuthState *state, const char *key,
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
41 int key_len, char **dest, int *dest_len)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
42 {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
43 DigestParams *digest = &state->digest_params;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
44
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
45 if (!strncmp(key, "realm=", key_len)) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
46 *dest = state->realm;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
47 *dest_len = sizeof(state->realm);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
48 } else if (!strncmp(key, "nonce=", key_len)) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
49 *dest = digest->nonce;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
50 *dest_len = sizeof(digest->nonce);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
51 } else if (!strncmp(key, "opaque=", key_len)) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
52 *dest = digest->opaque;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
53 *dest_len = sizeof(digest->opaque);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
54 } else if (!strncmp(key, "algorithm=", key_len)) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
55 *dest = digest->algorithm;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
56 *dest_len = sizeof(digest->algorithm);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
57 } else if (!strncmp(key, "qop=", key_len)) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
58 *dest = digest->qop;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
59 *dest_len = sizeof(digest->qop);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
60 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
61 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
62
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
63 static void handle_digest_update(HTTPAuthState *state, const char *key,
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
64 int key_len, char **dest, int *dest_len)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
65 {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
66 DigestParams *digest = &state->digest_params;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
67
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
68 if (!strncmp(key, "nextnonce=", key_len)) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
69 *dest = digest->nonce;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
70 *dest_len = sizeof(digest->nonce);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
71 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
72 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
73
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
74 static void choose_qop(char *qop, int size)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
75 {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
76 char *ptr = strstr(qop, "auth");
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
77 char *end = ptr + strlen("auth");
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
78
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
79 if (ptr && (!*end || isspace(*end) || *end == ',') &&
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
80 (ptr == qop || isspace(ptr[-1]) || ptr[-1] == ',')) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
81 av_strlcpy(qop, "auth", size);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
82 } else {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
83 qop[0] = 0;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
84 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
85 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
98 } else if (av_stristart(value, "Digest ", &p) &&
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
99 state->auth_type <= HTTP_AUTH_DIGEST) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
100 state->auth_type = HTTP_AUTH_DIGEST;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
101 memset(&state->digest_params, 0, sizeof(DigestParams));
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
105 choose_qop(state->digest_params.qop,
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
106 sizeof(state->digest_params.qop));
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
107 }
5885
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
114
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
115 static void update_md5_strings(struct AVMD5 *md5ctx, ...)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
116 {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
117 va_list vl;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
118
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
119 va_start(vl, md5ctx);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
120 while (1) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
121 const char* str = va_arg(vl, const char*);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
122 if (!str)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
123 break;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
124 av_md5_update(md5ctx, str, strlen(str));
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
125 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
126 va_end(vl);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
127 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
128
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
129 /* Generate a digest reply, according to RFC 2617. */
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
130 static char *make_digest_auth(HTTPAuthState *state, const char *username,
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
131 const char *password, const char *uri,
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
132 const char *method)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
133 {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
134 DigestParams *digest = &state->digest_params;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
135 int len;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
138 char nc[9];
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
139 int i;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
140 char A1hash[33], A2hash[33], response[33];
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
141 struct AVMD5 *md5ctx;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
142 uint8_t hash[16];
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
143 char *authstr;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
144
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
145 digest->nc++;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
146 snprintf(nc, sizeof(nc), "%08x", digest->nc);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
147
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
148 /* Generate a client nonce. */
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
151 ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
152 cnonce[2*sizeof(cnonce_buf)] = 0;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
153
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
154 md5ctx = av_malloc(av_md5_size);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
155 if (!md5ctx)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
156 return NULL;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
157
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
158 av_md5_init(md5ctx);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
159 update_md5_strings(md5ctx, username, ":", state->realm, ":", password, NULL);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
160 av_md5_final(md5ctx, hash);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
161 ff_data_to_hex(A1hash, hash, 16, 1);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
162 A1hash[32] = 0;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
163
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
164 if (!strcmp(digest->algorithm, "") || !strcmp(digest->algorithm, "MD5")) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
165 } else if (!strcmp(digest->algorithm, "MD5-sess")) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
166 av_md5_init(md5ctx);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
167 update_md5_strings(md5ctx, A1hash, ":", digest->nonce, ":", cnonce, NULL);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
168 av_md5_final(md5ctx, hash);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
169 ff_data_to_hex(A1hash, hash, 16, 1);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
170 A1hash[32] = 0;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
171 } else {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
172 /* Unsupported algorithm */
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
173 av_free(md5ctx);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
174 return NULL;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
175 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
176
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
177 av_md5_init(md5ctx);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
178 update_md5_strings(md5ctx, method, ":", uri, NULL);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
179 av_md5_final(md5ctx, hash);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
180 ff_data_to_hex(A2hash, hash, 16, 1);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
181 A2hash[32] = 0;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
182
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
183 av_md5_init(md5ctx);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
184 update_md5_strings(md5ctx, A1hash, ":", digest->nonce, NULL);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
185 if (!strcmp(digest->qop, "auth") || !strcmp(digest->qop, "auth-int")) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
186 update_md5_strings(md5ctx, ":", nc, ":", cnonce, ":", digest->qop, NULL);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
187 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
188 update_md5_strings(md5ctx, ":", A2hash, NULL);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
189 av_md5_final(md5ctx, hash);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
190 ff_data_to_hex(response, hash, 16, 1);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
191 response[32] = 0;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
192
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
193 av_free(md5ctx);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
194
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
195 if (!strcmp(digest->qop, "") || !strcmp(digest->qop, "auth")) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
196 } else if (!strcmp(digest->qop, "auth-int")) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
197 /* qop=auth-int not supported */
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
198 return NULL;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
199 } else {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
200 /* Unsupported qop value. */
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
201 return NULL;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
202 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
203
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
204 len = strlen(username) + strlen(state->realm) + strlen(digest->nonce) +
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
205 strlen(uri) + strlen(response) + strlen(digest->algorithm) +
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
206 strlen(digest->opaque) + strlen(digest->qop) + strlen(cnonce) +
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
207 strlen(nc) + 150;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
208
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
209 authstr = av_malloc(len);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
210 if (!authstr)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
211 return NULL;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
212 snprintf(authstr, len, "Authorization: Digest ");
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
213
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
214 /* TODO: Escape the quoted strings properly. */
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
215 av_strlcatf(authstr, len, "username=\"%s\"", username);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
216 av_strlcatf(authstr, len, ",realm=\"%s\"", state->realm);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
217 av_strlcatf(authstr, len, ",nonce=\"%s\"", digest->nonce);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
218 av_strlcatf(authstr, len, ",uri=\"%s\"", uri);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
219 av_strlcatf(authstr, len, ",response=\"%s\"", response);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
220 if (digest->algorithm[0])
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
221 av_strlcatf(authstr, len, ",algorithm=%s", digest->algorithm);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
222 if (digest->opaque[0])
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
223 av_strlcatf(authstr, len, ",opaque=\"%s\"", digest->opaque);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
224 if (digest->qop[0]) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
225 av_strlcatf(authstr, len, ",qop=\"%s\"", digest->qop);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
226 av_strlcatf(authstr, len, ",cnonce=\"%s\"", cnonce);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
227 av_strlcatf(authstr, len, ",nc=%s", nc);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
228 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
229
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
230 av_strlcatf(authstr, len, "\r\n");
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
231
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
232 return authstr;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
233 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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
4518f83661f4 Use AV_BASE64_SIZE() macro
hyc
parents: 6077
diff changeset
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
54d3a9240339 httpauth: Fix the length passed to av_strlcat
mstorsjo
parents: 6036
diff changeset
253 av_strlcat(ptr, "\r\n", len - (ptr - authstr));
5885
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
254 } else if (state->auth_type == HTTP_AUTH_DIGEST) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
255 char *username = av_strdup(auth), *password;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
256
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
257 if (!username)
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
258 return NULL;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
259
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
260 if ((password = strchr(username, ':'))) {
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
261 *password++ = 0;
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
262 authstr = make_digest_auth(state, username, password, path, method);
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
263 }
a1a309c4a751 Add support for http digest authentication
mstorsjo
parents: 5879
diff changeset
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