FFmpeg
Loading...
Searching...
No Matches
httpauth.c
Go to the documentation of this file.
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
22#include <stdbool.h>
23
24#include "httpauth.h"
25#include "libavutil/base64.h"
26#include "libavutil/avstring.h"
27#include "libavutil/mem.h"
28#include "internal.h"
30#include "libavutil/hash.h"
31#include "urldecode.h"
32
33/**
34 * Maximum length of a hash string in hexadecimal, with a string terminator
35 */
36#define HASH_STRING_LEN (AV_HASH_MAX_SIZE * 2 + 1)
37
38static void handle_basic_params(void *context, const char *key,
39 int key_len, char **dest, int *dest_len)
40{
41 HTTPAuthState *state = context;
42 if (!strncmp(key, "realm=", key_len)) {
43 *dest = state->realm;
44 *dest_len = sizeof(state->realm);
45 }
46}
47
48static void handle_digest_params(void *context, const char *key,
49 int key_len, char **dest, int *dest_len)
50{
51 HTTPAuthState *state = context;
52 DigestParams *digest = &state->digest_params;
53
54 if (!strncmp(key, "realm=", key_len)) {
55 *dest = state->realm;
56 *dest_len = sizeof(state->realm);
57 } else if (!strncmp(key, "nonce=", key_len)) {
58 *dest = digest->nonce;
59 *dest_len = sizeof(digest->nonce);
60 } else if (!strncmp(key, "opaque=", key_len)) {
61 *dest = digest->opaque;
62 *dest_len = sizeof(digest->opaque);
63 } else if (!strncmp(key, "algorithm=", key_len)) {
64 *dest = digest->algorithm;
65 *dest_len = sizeof(digest->algorithm);
66 } else if (!strncmp(key, "qop=", key_len)) {
67 *dest = digest->qop;
68 *dest_len = sizeof(digest->qop);
69 } else if (!strncmp(key, "stale=", key_len)) {
70 *dest = digest->stale;
71 *dest_len = sizeof(digest->stale);
72 }
73}
74
75static void handle_digest_update(void *context, const char *key,
76 int key_len, char **dest, int *dest_len)
77{
78 HTTPAuthState *state = context;
79 DigestParams *digest = &state->digest_params;
80
81 if (!strncmp(key, "nextnonce=", key_len)) {
82 *dest = digest->nonce;
83 *dest_len = sizeof(digest->nonce);
84 }
85}
86
87static void choose_qop(char *qop, int size)
88{
89 char *ptr = strstr(qop, "auth");
90 char *end = ptr + strlen("auth");
91
92 if (ptr && (!*end || av_isspace(*end) || *end == ',') &&
93 (ptr == qop || av_isspace(ptr[-1]) || ptr[-1] == ',')) {
94 av_strlcpy(qop, "auth", size);
95 } else {
96 qop[0] = 0;
97 }
98}
99
101 const char *value)
102{
103 if (!av_strcasecmp(key, "WWW-Authenticate") || !av_strcasecmp(key, "Proxy-Authenticate")) {
104 const char *p;
105 if (av_stristart(value, "Basic ", &p) &&
106 state->auth_type <= HTTP_AUTH_BASIC) {
107 state->auth_type = HTTP_AUTH_BASIC;
108 state->realm[0] = 0;
109 state->stale = 0;
111 } else if (av_stristart(value, "Digest ", &p) &&
112 state->auth_type <= HTTP_AUTH_DIGEST) {
113 state->auth_type = HTTP_AUTH_DIGEST;
114 memset(&state->digest_params, 0, sizeof(DigestParams));
115 state->realm[0] = 0;
116 state->stale = 0;
118 choose_qop(state->digest_params.qop,
119 sizeof(state->digest_params.qop));
120 if (!av_strcasecmp(state->digest_params.stale, "true"))
121 state->stale = 1;
122 }
123 } else if (!av_strcasecmp(key, "Authentication-Info")) {
125 }
126}
127
128static void update_hash_strings(struct AVHashContext *hashctx, ...)
129{
130 va_list vl;
131
132 va_start(vl, hashctx);
133 while (1) {
134 const char* str = va_arg(vl, const char*);
135 if (!str)
136 break;
137 av_hash_update(hashctx, str, strlen(str));
138 }
139 va_end(vl);
140}
141
142/* Generate a digest reply, according to RFC 2617 and RFC 7616. */
143static char *make_digest_auth(HTTPAuthState *state, const char *username,
144 const char *password, const char *uri,
145 const char *method)
146{
147 DigestParams *digest = &state->digest_params;
148 int len;
149 uint32_t cnonce_buf[2];
150 char cnonce[17];
151 char nc[9];
152 int i;
153 char A1hash[HASH_STRING_LEN], A2hash[HASH_STRING_LEN], response[HASH_STRING_LEN];
154 struct AVHashContext *hashctx = NULL;
155 uint8_t hash[AV_HASH_MAX_SIZE];
156 const char* algorithm = NULL;
157 bool is_session = false;
158 int hash_size;
159 char *authstr;
160
161 digest->nc++;
162 snprintf(nc, sizeof(nc), "%08x", digest->nc);
163
164 if (!strcmp(digest->algorithm, "")) { algorithm = "MD5"; }
165 else if (!strcmp(digest->algorithm, "MD5")) { algorithm = "MD5"; }
166 else if (!strcmp(digest->algorithm, "MD5-sess")) { algorithm = "MD5"; is_session = true; }
167 else if (!strcmp(digest->algorithm, "SHA-256")) { algorithm = "SHA256"; }
168 else if (!strcmp(digest->algorithm, "SHA-256-sess")) { algorithm = "SHA256"; is_session = true; }
169 else if (!strcmp(digest->algorithm, "SHA-512-256")) { algorithm = "SHA512/256"; }
170 else if (!strcmp(digest->algorithm, "SHA-512-256-sess")){ algorithm = "SHA512/256"; is_session = true; }
171 else {
172 /* Unsupported algorithm */
173 return NULL;
174 }
175
176 /* Generate a client nonce. */
177 for (i = 0; i < 2; i++)
178 cnonce_buf[i] = av_get_random_seed();
179 ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1);
180
181 if (av_hash_alloc(&hashctx, algorithm) < 0)
182 return NULL;
183
184 hash_size = av_hash_get_size(hashctx);
185
186 av_hash_init (hashctx);
187 update_hash_strings(hashctx, username, ":", state->realm, ":", password, NULL);
188 av_hash_final(hashctx, hash);
189 ff_data_to_hex(A1hash, hash, hash_size, 1);
190
191 if (is_session) {
192 av_hash_init(hashctx);
193 update_hash_strings(hashctx, A1hash, ":", digest->nonce, ":", cnonce, NULL);
194 av_hash_final(hashctx, hash);
195 ff_data_to_hex(A1hash, hash, hash_size, 1);
196 }
197
198 av_hash_init(hashctx);
199 update_hash_strings(hashctx, method, ":", uri, NULL);
200 av_hash_final(hashctx, hash);
201 ff_data_to_hex(A2hash, hash, hash_size, 1);
202
203 av_hash_init(hashctx);
204 update_hash_strings(hashctx, A1hash, ":", digest->nonce, NULL);
205 if (!strcmp(digest->qop, "auth") || !strcmp(digest->qop, "auth-int")) {
206 update_hash_strings(hashctx, ":", nc, ":", cnonce, ":", digest->qop, NULL);
207 }
208 update_hash_strings(hashctx, ":", A2hash, NULL);
209 av_hash_final(hashctx, hash);
210 ff_data_to_hex(response, hash, hash_size, 1);
211
212 av_hash_freep(&hashctx);
213
214 if (!strcmp(digest->qop, "") || !strcmp(digest->qop, "auth")) {
215 } else if (!strcmp(digest->qop, "auth-int")) {
216 /* qop=auth-int not supported */
217 return NULL;
218 } else {
219 /* Unsupported qop value. */
220 return NULL;
221 }
222
223 len = strlen(username) + strlen(state->realm) + strlen(digest->nonce) +
224 strlen(uri) + strlen(response) + strlen(digest->algorithm) +
225 strlen(digest->opaque) + strlen(digest->qop) + strlen(cnonce) +
226 strlen(nc) + 150;
227
228 authstr = av_malloc(len);
229 if (!authstr)
230 return NULL;
231 snprintf(authstr, len, "Authorization: Digest ");
232
233 /* TODO: Escape the quoted strings properly. */
234 av_strlcatf(authstr, len, "username=\"%s\"", username);
235 av_strlcatf(authstr, len, ", realm=\"%s\"", state->realm);
236 av_strlcatf(authstr, len, ", nonce=\"%s\"", digest->nonce);
237 av_strlcatf(authstr, len, ", uri=\"%s\"", uri);
238 av_strlcatf(authstr, len, ", response=\"%s\"", response);
239
240 // we are violating the RFC and use "" because all others seem to do that too.
241 if (digest->algorithm[0])
242 av_strlcatf(authstr, len, ", algorithm=\"%s\"", digest->algorithm);
243
244 if (digest->opaque[0])
245 av_strlcatf(authstr, len, ", opaque=\"%s\"", digest->opaque);
246 if (digest->qop[0]) {
247 av_strlcatf(authstr, len, ", qop=\"%s\"", digest->qop);
248 av_strlcatf(authstr, len, ", cnonce=\"%s\"", cnonce);
249 av_strlcatf(authstr, len, ", nc=%s", nc);
250 }
251
252 av_strlcatf(authstr, len, "\r\n");
253
254 return authstr;
255}
256
258 const char *path, const char *method)
259{
260 char *authstr = NULL;
261
262 /* Clear the stale flag, we assume the auth is ok now. It is reset
263 * by the server headers if there's a new issue. */
264 state->stale = 0;
265 if (!auth || !strchr(auth, ':'))
266 return NULL;
267
268 if (state->auth_type == HTTP_AUTH_BASIC) {
269 int auth_b64_len, len;
270 char *ptr, *decoded_auth = ff_urldecode(auth, 0);
271
272 if (!decoded_auth)
273 return NULL;
274
275 auth_b64_len = AV_BASE64_SIZE(strlen(decoded_auth));
276 len = auth_b64_len + 30;
277
278 authstr = av_malloc(len);
279 if (!authstr) {
280 av_free(decoded_auth);
281 return NULL;
282 }
283
284 snprintf(authstr, len, "Authorization: Basic ");
285 ptr = authstr + strlen(authstr);
286 av_base64_encode(ptr, auth_b64_len, decoded_auth, strlen(decoded_auth));
287 av_strlcat(ptr, "\r\n", len - (ptr - authstr));
288 av_free(decoded_auth);
289 } else if (state->auth_type == HTTP_AUTH_DIGEST) {
290 char *username = ff_urldecode(auth, 0), *password;
291
292 if (!username)
293 return NULL;
294
295 if ((password = strchr(username, ':'))) {
296 *password++ = 0;
297 authstr = make_digest_auth(state, username, password, path, method);
298 }
299 av_free(username);
300 }
301 return authstr;
302}
static uint8_t hash[HASH_SIZE]
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
double value
Definition eval.c:102
static struct @346255127015250356166251341105367306144006377143 state
const char * key
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition base64.c:152
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition base64.h:66
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
void av_hash_freep(AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
Definition hash.c:248
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition hash.c:151
void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
Update a hash context with additional data.
Definition hash.c:172
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition hash.c:114
void av_hash_final(AVHashContext *ctx, uint8_t *dst)
Finalize a hash context and compute the actual hash value.
Definition hash.c:193
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition hash.h:156
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
Definition avstring.c:95
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition avstring.h:218
int av_stristart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str independent of case.
Definition avstring.c:47
int av_hash_get_size(const AVHashContext *ctx)
Definition hash.c:109
Generic hashing API.
#define HASH_STRING_LEN
Maximum length of a hash string in hexadecimal, with a string terminator.
Definition httpauth.c:36
char * ff_http_auth_create_response(HTTPAuthState *state, const char *auth, const char *path, const char *method)
Definition httpauth.c:257
static void update_hash_strings(struct AVHashContext *hashctx,...)
Definition httpauth.c:128
static void choose_qop(char *qop, int size)
Definition httpauth.c:87
static void handle_basic_params(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition httpauth.c:38
static void handle_digest_params(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition httpauth.c:48
static void handle_digest_update(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition httpauth.c:75
static char * make_digest_auth(HTTPAuthState *state, const char *username, const char *password, const char *uri, const char *method)
Definition httpauth.c:143
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, const char *value)
Definition httpauth.c:100
@ HTTP_AUTH_DIGEST
HTTP 1.1 Digest auth from RFC 2617.
Definition httpauth.h:32
@ HTTP_AUTH_BASIC
HTTP 1.0 Basic auth from RFC 1945 (also in RFC 2617)
Definition httpauth.h:30
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition utils.c:473
void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf, void *context)
Parse a string with comma-separated key=value pairs.
Definition utils.c:523
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
char algorithm[sizeof("SHA-512-256-sess")]
Server specified digest algorithm.
Definition httpauth.h:37
int nc
Nonce count, the number of earlier replies where this particular nonce has been used.
Definition httpauth.h:47
char stale[10]
The server indicated that the auth was ok, but needs to be redone with a new, non-stale nonce.
Definition httpauth.h:44
char qop[30]
Quality of protection, containing the one that we've chosen to use, from the alternatives that the se...
Definition httpauth.h:38
char nonce[300]
Server specified nonce.
Definition httpauth.h:36
char opaque[300]
A server-specified string that should be included in authentication responses, not included in the ac...
Definition httpauth.h:41
HTTP Authentication state structure.
Definition httpauth.h:55
#define av_free(p)
int size
char * ff_urldecode(const char *url, int decode_plus_sign)
Decodes an URL from its percent-encoded form back into normal representation.
Definition urldecode.c:80
int len