-
Notifications
You must be signed in to change notification settings - Fork 40
/
httpauth.c
451 lines (388 loc) · 10.9 KB
/
httpauth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* $Id$ */
/*
* Copyright (c) 2015, 2017 Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "kcgi.h"
#include "extern.h"
/*
* A sequence of bytes parsed from the input stream.
*/
struct pdigbuf {
const char *pos;
size_t sz;
};
/*
* Pointers to the components of an HTTP digest scheme in the input
* stream.
* These are empty strings (sz == 0) if undescribed.
*/
struct pdigest {
enum khttpalg alg;
enum khttpqop qop;
struct pdigbuf user;
struct pdigbuf uri;
struct pdigbuf realm;
struct pdigbuf nonce;
struct pdigbuf cnonce;
struct pdigbuf response;
struct pdigbuf opaque;
uint32_t count;
};
/*
* Hash algorithm identifiers.
*/
static const char *const httpalgs[KHTTPALG__MAX] = {
"MD5", /* KHTTPALG_MD5 */
"MD5-sess" /* KHTTPALG_MD5_SESS */
};
/*
* Quality-of-protection identifiers.
*/
static const char *const httpqops[KHTTPQOP__MAX] = {
NULL, /* KHTTPQOP_NONE */
"auth", /* KHTTPQOP_AUTH */
"auth-int" /* KHTTPQOP_AUTH_INT */
};
/*
* Parses the next token part---a sequence of non-whitespace (and
* non-"delim", with '\0' being the noop delimeter) characters---after
* skipping leading whitespace, then positions "next" to be at the next
* token (after any whitespace).
* Returns the start of the token and its size.
*/
static const char *
kauth_nexttok(const char **next, char delim, size_t *sz)
{
const char *cp;
/* Skip past leading white-space. */
while (isspace((unsigned char)**next))
(*next)++;
/* Scan til whitespace or delimiter. */
cp = *next;
while ('\0' != **next && delim != **next &&
! isspace((unsigned char)**next))
(*next)++;
*sz = *next - cp;
/* Scan after delimiter, if applicable. */
if ('\0' != delim && delim == **next)
(*next)++;
/* Scan til next non-whitespace. */
while (isspace((unsigned char)**next))
(*next)++;
return(cp);
}
/*
* Parse a quoted-pair (or non-quoted pair) from the string "cp".
* Puts the location of the next token back into "cp" and fills the
* pointer in "val" (if non-NULL) and its size in "sz".
*/
static void
kauth_nextvalue(struct pdigbuf *val, const char **cp)
{
int quot;
if ('\0' == **cp)
return;
if ((quot = ('"' == **cp)))
(*cp)++;
if (NULL != val) {
val->pos = *cp;
val->sz = 0;
}
for ( ; '\0' != **cp; (*cp)++) {
if (quot && '"' == **cp && '\\' != (*cp)[-1])
break;
else if ( ! quot && isspace((unsigned char)**cp))
break;
else if ( ! quot && ',' == **cp)
break;
if (NULL != val)
val->sz++;
}
if (quot && '"' == **cp)
(*cp)++;
while (isspace((unsigned char)**cp))
(*cp)++;
if (',' == **cp)
(*cp)++;
while (isspace((unsigned char)**cp))
(*cp)++;
}
/*
* Parse a token.
* We don't strictly follow RFC 2615's TOKEN specification, which says
* that tokens can be followed by any separator.
* We only use commas as separators.
*/
static void
kauth_nexttoken(size_t *val, const char **cp,
const char *const *vals, size_t valsz)
{
struct pdigbuf buf;
memset(&buf, 0, sizeof(struct pdigbuf));
kauth_nextvalue(&buf, cp);
for (*val = 0; *val < valsz; (*val)++) {
if (NULL == vals[*val])
continue;
if (buf.sz != strlen(vals[*val]))
continue;
if (0 == strncasecmp(buf.pos, vals[*val], buf.sz))
return;
}
}
static void
kauth_alg(enum khttpalg *val, const char **cp)
{
size_t i;
kauth_nexttoken(&i, cp, httpalgs, KHTTPALG__MAX);
*val = i;
}
static void
kauth_qop(enum khttpqop *val, const char **cp)
{
size_t i;
kauth_nexttoken(&i, cp, httpqops, KHTTPQOP__MAX);
*val = i;
}
/*
* Parse the 8-byte nonce count ("nc") value.
* See RFC 7616 section 3.4.
*/
static void
kauth_count(uint32_t *count, const char **cp)
{
struct pdigbuf buf;
char numbuf[9];
char *ep;
unsigned long long ulval;
*count = 0;
memset(&buf, 0, sizeof(struct pdigbuf));
/* According to the RFC, this is 8 bytes long. */
kauth_nextvalue(&buf, cp);
if (buf.sz != 8)
return;
/* Copy into a NUL-terminated buffer. */
memcpy(numbuf, buf.pos, buf.sz);
numbuf[buf.sz] = '\0';
/*
* Convert from the hex string into a number.
* There are a maximum of 8 possible digits in this hex value,
* so we'll have no more than 0xffffffff.
* Default to zero if there are errors.
* Note: UINT32_MAX < long long int maximum.
*/
errno = 0;
ulval = strtoull(numbuf, &ep, 16);
if (numbuf[0] == '\0' || *ep != '\0')
*count = 0;
else if (errno == ERANGE && ulval == ULLONG_MAX)
*count = 0;
else if (ulval > UINT32_MAX)
*count = 0;
else
*count = ulval;
}
static int
kauth_eq(const char *p, const char *start, size_t sz, size_t want)
{
return(want == sz && 0 == strncasecmp(start, p, want));
}
/*
* The "bearer" or "basic" authentication is just that word followed by
* opaque text. HTTP "basic" authentication has a well-defined username
* and password structure, but "bearer" is opaque. These are both
* handled by the calling context: we don't do any validation here.
*/
static void
khttpbasic_input(int fd, const char *cp, enum kauth auth)
{
int authorised;
fullwrite(fd, &auth, sizeof(enum kauth));
while (isspace((unsigned char)*cp))
cp++;
if ('\0' == *cp) {
authorised = 0;
fullwrite(fd, &authorised, sizeof(int));
return;
}
authorised = 1;
fullwrite(fd, &authorised, sizeof(int));
fullwriteword(fd, cp);
}
/*
* Parse HTTP ``Digest'' authentication tokens from the NUL-terminated
* string, which can be NULL or malformed.
*/
static int
khttpdigest_input(int fd, const char *cp)
{
enum kauth auth;
const char *start;
int rc, authorised;
size_t sz;
struct pdigest d;
auth = KAUTH_DIGEST;
fullwrite(fd, &auth, sizeof(enum kauth));
memset(&d, 0, sizeof(struct pdigest));
for (rc = 1; 1 == rc && '\0' != *cp; ) {
start = kauth_nexttok(&cp, '=', &sz);
if (kauth_eq("username", start, sz, 8))
kauth_nextvalue(&d.user, &cp);
else if (kauth_eq("realm", start, sz, 5))
kauth_nextvalue(&d.realm, &cp);
else if (kauth_eq("nonce", start, sz, 5))
kauth_nextvalue(&d.nonce, &cp);
else if (kauth_eq("cnonce", start, sz, 6))
kauth_nextvalue(&d.cnonce, &cp);
else if (kauth_eq("response", start, sz, 8))
kauth_nextvalue(&d.response, &cp);
else if (kauth_eq("uri", start, sz, 3))
kauth_nextvalue(&d.uri, &cp);
else if (kauth_eq("algorithm", start, sz, 9))
kauth_alg(&d.alg, &cp);
else if (kauth_eq("qop", start, sz, 3))
kauth_qop(&d.qop, &cp);
else if (kauth_eq("nc", start, sz, 2))
kauth_count(&d.count, &cp);
else if (kauth_eq("opaque", start, sz, 6))
kauth_nextvalue(&d.opaque, &cp);
else
kauth_nextvalue(NULL, &cp);
}
/* Minimum requirements. */
authorised =
0 != d.user.sz &&
0 != d.realm.sz &&
0 != d.nonce.sz &&
0 != d.response.sz &&
0 != d.uri.sz;
/* Additional requirements: MD5-sess. */
if (authorised && KHTTPALG_MD5_SESS == d.alg)
authorised = 0 != d.cnonce.sz;
/* Additional requirements: qop. */
if (authorised &&
(KHTTPQOP_AUTH == d.qop ||
KHTTPQOP_AUTH_INT == d.qop))
authorised =
0 != d.count &&
0 != d.cnonce.sz;
fullwrite(fd, &authorised, sizeof(int));
if ( ! authorised)
return(0);
fullwrite(fd, &d.alg, sizeof(enum khttpalg));
fullwrite(fd, &d.qop, sizeof(enum khttpqop));
fullwrite(fd, &d.user.sz, sizeof(size_t));
fullwrite(fd, d.user.pos, d.user.sz);
fullwrite(fd, &d.uri.sz, sizeof(size_t));
fullwrite(fd, d.uri.pos, d.uri.sz);
fullwrite(fd, &d.realm.sz, sizeof(size_t));
fullwrite(fd, d.realm.pos, d.realm.sz);
fullwrite(fd, &d.nonce.sz, sizeof(size_t));
fullwrite(fd, d.nonce.pos, d.nonce.sz);
fullwrite(fd, &d.cnonce.sz, sizeof(size_t));
fullwrite(fd, d.cnonce.pos, d.cnonce.sz);
fullwrite(fd, &d.response.sz, sizeof(size_t));
fullwrite(fd, d.response.pos, d.response.sz);
fullwrite(fd, &d.count, sizeof(uint32_t));
fullwrite(fd, &d.opaque.sz, sizeof(size_t));
fullwrite(fd, d.opaque.pos, d.opaque.sz);
/* Do we need to MD5-hash our contents? */
return(KHTTPQOP_AUTH_INT == d.qop);
}
enum kcgi_err
kworker_auth_parent(int fd, struct khttpauth *auth)
{
enum kcgi_err ke;
if (fullread(fd, &auth->type, sizeof(enum kauth), 0, &ke) < 0)
return ke;
switch (auth->type) {
case KAUTH_DIGEST:
if (fullread(fd, &auth->authorised, sizeof(int), 0, &ke) < 0)
return ke;
if (!auth->authorised)
break;
if (fullread(fd, &auth->d.digest.alg, sizeof(enum khttpalg), 0, &ke) < 0)
return ke;
if (fullread(fd, &auth->d.digest.qop, sizeof(enum khttpqop), 0, &ke) < 0)
return ke;
if ((ke = fullreadword(fd, &auth->d.digest.user)) != KCGI_OK)
return ke;
if ((ke = fullreadword(fd, &auth->d.digest.uri)) != KCGI_OK)
return ke;
if ((ke = fullreadword(fd, &auth->d.digest.realm)) != KCGI_OK)
return ke;
if ((ke = fullreadword(fd, &auth->d.digest.nonce)) != KCGI_OK)
return ke;
if ((ke = fullreadword(fd, &auth->d.digest.cnonce)) != KCGI_OK)
return ke;
if ((ke = fullreadword(fd, &auth->d.digest.response)) != KCGI_OK)
return ke;
if (fullread(fd, &auth->d.digest.count, sizeof(uint32_t), 0, &ke) < 0)
return ke;
if ((ke = fullreadword(fd, &auth->d.digest.opaque)) != KCGI_OK)
return ke;
break;
case KAUTH_BASIC:
case KAUTH_BEARER:
if (fullread(fd, &auth->authorised, sizeof(int), 0, &ke) < 0)
return ke;
if (!auth->authorised)
break;
if ((ke = fullreadword(fd, &auth->d.basic.response)) != KCGI_OK)
return ke;
break;
default:
break;
}
return KCGI_OK;
}
/*
* Parse the "basic", "digest", or "bearer" authorisation from the request.
* We return non-zero if the body of the request needs to be MD5-hashed,
* i.e., if we have auth-int digest QOP.
*/
int
kworker_auth_child(int fd, const char *cp)
{
const char *start;
size_t sz;
enum kauth auth;
if (cp == NULL || *cp == '\0') {
auth = KAUTH_NONE;
fullwrite(fd, &auth, sizeof(enum kauth));
return 0;
}
start = kauth_nexttok(&cp, '\0', &sz);
if (sz == 6 && strncasecmp(start, "bearer", sz) == 0) {
khttpbasic_input(fd, cp, KAUTH_BEARER);
return 0;
} else if (sz == 5 && strncasecmp(start, "basic", sz) == 0) {
khttpbasic_input(fd, cp, KAUTH_BASIC);
return 0;
} else if (sz == 6 && strncasecmp(start, "digest", sz) == 0)
return khttpdigest_input(fd, cp);
auth = KAUTH_UNKNOWN;
fullwrite(fd, &auth, sizeof(enum kauth));
return 0;
}