aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/gen_auth.c
blob: e993fb2011d1208e095b4ff44d23733b09b569f6 (plain) (blame)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
  This file is part of libmicrohttpd
  Copyright (C) 2022 Evgeny Grin (Karlson2k)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.
  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file microhttpd/gen_auth.c
 * @brief  HTTP authorisation general functions
 * @author Karlson2k (Evgeny Grin)
 */

#include "gen_auth.h"
#include "internal.h"
#include "connection.h"
#include "mhd_str.h"
#include "mhd_assert.h"

#ifdef BAUTH_SUPPORT
#include "basicauth.h"
#endif /* BAUTH_SUPPORT */
#ifdef DAUTH_SUPPORT
#include "digestauth.h"
#endif /* DAUTH_SUPPORT */

#if ! defined(BAUTH_SUPPORT) && ! defined(DAUTH_SUPPORT)
#error This file requires Basic or Digest authentication support
#endif

#ifdef BAUTH_SUPPORT
/**
 * Parse request Authorization header parameters for Basic Authentication
 * @param str the header string, everything after "Basic " substring
 * @param str_len the length of @a str in characters
 * @param[out] pbauth the pointer to the structure with Basic Authentication
 *               parameters
 * @return true if parameters has been successfully parsed,
 *         false if format of the @a str is invalid
 */
static bool
parse_bauth_params (const char *str,
                    size_t str_len,
                    struct MHD_RqBAuth *pbauth)
{
  size_t i;

  i = 0;

  /* Skip all whitespaces at start */
  while (i < str_len && (' ' == str[i] || '\t' == str[i]))
    i++;

  if (str_len > i)
  {
    size_t token68_start;
    size_t token68_len;

    /* 'i' points to the first non-whitespace char after scheme token */
    token68_start = i;
    /* Find end of the token. Token cannot contain whitespace. */
    while (i < str_len && ' ' != str[i] && '\t' != str[i])
    {
      if (0 == str[0])
        return false; /* Binary zero is not allowed */
      i++;
    }
    token68_len = i - token68_start;
    mhd_assert (0 != token68_len);

    /* Skip all whitespaces */
    while (i < str_len && (' ' == str[i] || '\t' == str[i]))
      i++;
    /* Check whether any garbage is present at the end of the string */
    if (str_len != i)
      return false;
    else
    {
      /* No more data in the string, only single token68. */
      const struct _MHD_cstr_w_len tkn = { str + token68_start, token68_len};
      memcpy (&pbauth->token68, &tkn, sizeof(tkn));
    }
  }
  return true;
}


#endif /* BAUTH_SUPPORT */

#ifdef DAUTH_SUPPORT

/**
 * Helper structure to map token name to position where to put token's value
 */
struct dauth_token_param
{
  const struct _MHD_cstr_w_len *const tk_name;
  struct MHD_RqDAuthParam *const param;
};

/**
 * Parse request Authorization header parameters for Digest Authentication
 * @param str the header string, everything after "Digest " substring
 * @param str_len the length of @a str in characters
 * @param[out] pdauth the pointer to the structure with Digest Authentication
 *               parameters
 * @return true if parameters has been successfully parsed,
 *         false if format of the @a str is invalid
 */
static bool
parse_dauth_params (const char *str,
                    const size_t str_len,
                    struct MHD_RqDAuth *pdauth)
{
  static const struct _MHD_cstr_w_len nonce_tk = _MHD_S_STR_W_LEN ("nonce");
  static const struct _MHD_cstr_w_len opaque_tk = _MHD_S_STR_W_LEN ("opaque");
  static const struct _MHD_cstr_w_len algorithm_tk =
    _MHD_S_STR_W_LEN ("algorithm");
  static const struct _MHD_cstr_w_len response_tk =
    _MHD_S_STR_W_LEN ("response");
  static const struct _MHD_cstr_w_len username_tk =
    _MHD_S_STR_W_LEN ("username");
  static const struct _MHD_cstr_w_len username_ext_tk =
    _MHD_S_STR_W_LEN ("username*");
  static const struct _MHD_cstr_w_len realm_tk = _MHD_S_STR_W_LEN ("realm");
  static const struct _MHD_cstr_w_len uri_tk = _MHD_S_STR_W_LEN ("uri");
  static const struct _MHD_cstr_w_len qop_tk = _MHD_S_STR_W_LEN ("qop");
  static const struct _MHD_cstr_w_len cnonce_tk = _MHD_S_STR_W_LEN ("cnonce");
  static const struct _MHD_cstr_w_len nc_tk = _MHD_S_STR_W_LEN ("nc");
  static const struct _MHD_cstr_w_len userhash_tk =
    _MHD_S_STR_W_LEN ("userhash");
  struct MHD_RqDAuthParam userhash;
  struct dauth_token_param map[] = {
    {&nonce_tk, &(pdauth->nonce)},
    {&opaque_tk, &(pdauth->opaque)},
    {&algorithm_tk, &(pdauth->algorithm)},
    {&response_tk, &(pdauth->response)},
    {&username_tk, &(pdauth->username)},
    {&username_ext_tk, &(pdauth->username_ext)},
    {&realm_tk, &(pdauth->realm)},
    {&uri_tk, &(pdauth->uri)},
    {&qop_tk, &(pdauth->qop)},
    {&cnonce_tk, &(pdauth->cnonce)},
    {&nc_tk, &(pdauth->nc)},
    {&userhash_tk, &userhash}
  };
  size_t i;
  size_t p;

  memset (&userhash, 0, sizeof(userhash));
  i = 0;

  /* Skip all whitespaces at start */
  while (i < str_len && (' ' == str[i] || '\t' == str[i]))
    i++;

  while (str_len > i)
  {
    size_t left;
    mhd_assert (' ' != str[i]);
    mhd_assert ('\t' != str[i]);

    left = str_len - i;
    for (p = 0; p < sizeof(map) / sizeof(map[0]); p++)
    {
      struct dauth_token_param *const aparam = map + p;
      if ( (aparam->tk_name->len < left) &&
           MHD_str_equal_caseless_bin_n_ (str + i, aparam->tk_name->str,
                                          aparam->tk_name->len) &&
           (('=' == str[i + aparam->tk_name->len]) ||
            (' ' == str[i + aparam->tk_name->len]) ||
            ('\t' == str[i + aparam->tk_name->len])) )
      {
        size_t value_start;
        size_t value_len;
        bool quoted; /* Only mark as "quoted" if backslash-escape used */

        quoted = false;
        i += aparam->tk_name->len;
        /* Skip all whitespaces before '=' */
        while (str_len > i && (' ' == str[i] || '\t' == str[i]))
          i++;
        if ((i == str_len) || ('=' != str[i]))
          return false; /* No equal sign, broken data */
        i++;
        /* Skip all whitespaces after '=' */
        while (str_len > i && (' ' == str[i] || '\t' == str[i]))
          i++;
        if ((str_len > i) && ('"' == str[i]))
        { /* Value is in quotation marks */
          i++; /* Advance after the opening quote */
          value_start = i;
          while (str_len > i && '"' != str[i])
          {
            if ('\\' == str[i])
            {
              i++;
              quoted = true; /* Have escaped chars */
            }
            if (0 == str[i])
              return false; /* Binary zero in parameter value */
            i++;
          }
          if (str_len <= i)
            return false; /* No closing quote */
          mhd_assert ('"' == str[i]);
          value_len = i - value_start;
          i++; /* Advance after the closing quote */
        }
        else
        {
          value_start = i;
          while (str_len > i && ',' != str[i] &&
                 ' ' != str[i] && '\t' != str[i] && ';' != str[i])
          {
            if (0 == str[i])
              return false;  /* Binary zero in parameter value */
            i++;
          }
          value_len = i - value_start;
        }
        /* Skip all whitespaces after parameter value */
        while (str_len > i && (' ' == str[i] || '\t' == str[i]))
          i++;
        if ((str_len > i) && (',' != str[i]))
          return false; /* Garbage after parameter value */

        /* Have valid parameter name and value */
        mhd_assert (! quoted || 0 != value_len);
        if (1)
        {
          const struct _MHD_cstr_w_len val = {str + value_start, value_len};
          memcpy (&aparam->param->value, &val, sizeof(val));
        }
        aparam->param->quoted = quoted;

        break; /* Found matching parameter name */
      }
    }
    if (p == sizeof(map) / sizeof(map[0]))
    {
      /* No matching parameter name */
      while (str_len > i && ',' != str[i])
      {
        if ('"' == str[i])
        { /* Skip quoted part */
          i++; /* Advance after the opening quote */
          while (str_len > i && '"' != str[i])
          {
            if ('\\' == str[i])
              i++; /* Skip escaped char */
            i++;
          }
          if (str_len <= i)
            return false; /* No closing quote */
          mhd_assert ('"' == str[i]);
        }
        i++;
      }
    }
    mhd_assert (str_len == i || ',' == str[i]);
    if (str_len > i)
      i++; /* Advance after ',' */
    /* Skip all whitespaces before next parameter name */
    while (i < str_len && (' ' == str[i] || '\t' == str[i]))
      i++;
  }

  /* Postprocess values */
  if ((NULL != userhash.value.str) && (0 != userhash.value.len))
  {
    const char *param_str;
    size_t param_len;
    char buf[5 * 2]; /* 5 is the length of "false" (longer then "true") */
    if (! userhash.quoted)
    {
      param_str = userhash.value.str;
      param_len = userhash.value.len;
    }
    else
    {
      if (sizeof(buf) / sizeof(buf[0]) >= userhash.value.len)
      {
        param_len = MHD_str_unquote (userhash.value.str, userhash.value.len,
                                     buf);
        param_str = buf;
      }
      else
        param_len = 0;
    }
    if ((param_len == 4) && MHD_str_equal_caseless_bin_n_ (param_str, "true",
                                                           4))
      pdauth->userhash = true;
    else
      pdauth->userhash = false;
  }
  else
    pdauth->userhash = false;

  return true;
}


#endif /* DAUTH_SUPPORT */


/**
 * Parse request "Authorization" header
 * @param c the connection to process
 * @return true if any supported Authorisation scheme were found,
 *         false if no "Authorization" header found, no supported scheme found,
 *         or an error occurred.
 */
_MHD_static_inline bool
parse_auth_rq_header_ (struct MHD_Connection *c)
{
  const char *h; /**< The "Authorization" header */
  size_t h_len;
  struct MHD_AuthRqHeader *rq_auth;
  size_t i;

  mhd_assert (NULL == c->rq_auth);
  mhd_assert (MHD_CONNECTION_HEADERS_PROCESSED <= c->state);
  if (MHD_CONNECTION_HEADERS_PROCESSED > c->state)
    return false;

  if (MHD_NO ==
      MHD_lookup_connection_value_n (c, MHD_HEADER_KIND,
                                     MHD_HTTP_HEADER_AUTHORIZATION,
                                     MHD_STATICSTR_LEN_ ( \
                                       MHD_HTTP_HEADER_AUTHORIZATION), &h,
                                     &h_len))
  {
    rq_auth = (struct MHD_AuthRqHeader *)
              MHD_connection_alloc_memory_ (c,
                                            sizeof (struct MHD_AuthRqHeader));
    c->rq_auth = rq_auth;
    if (NULL != rq_auth)
    {
      memset (rq_auth, 0, sizeof(struct MHD_AuthRqHeader));
      rq_auth->auth_type = MHD_AUTHTYPE_NONE;
    }
    return false;
  }

  rq_auth = NULL;
  i = 0;
  /* Skip the leading whitespace */
  while (i < h_len)
  {
    const char ch = h[i];
    if ((' ' != ch) && ('\t' != ch))
      break;
    i++;
  }
  h += i;
  h_len -= i;

#ifdef DAUTH_SUPPORT
  if (1)
  {
    static const struct _MHD_cstr_w_len scheme_token =
      _MHD_S_STR_W_LEN (_MHD_AUTH_DIGEST_BASE);

    if ((scheme_token.len <= h_len) &&
        MHD_str_equal_caseless_bin_n_ (h, scheme_token.str, scheme_token.len))
    {
      i = scheme_token.len;
      /* RFC 7235 require only space after scheme token */
      if ( (h_len <= i) ||
           ((' ' == h[i]) || ('\t' == h[i])) ) /* Actually tab should NOT be allowed */
      { /* Matched Digest authorisation scheme */
        i++; /* Advance to the next char (even if it is beyond the end of the string) */

        rq_auth = (struct MHD_AuthRqHeader *)
                  MHD_connection_alloc_memory_ (c,
                                                sizeof (struct MHD_AuthRqHeader)
                                                + sizeof (struct MHD_RqDAuth));
        c->rq_auth = rq_auth;
        if (NULL == rq_auth)
        {
#ifdef HAVE_MESSAGES
          MHD_DLOG (c->daemon,
                    _ ("Failed to allocate memory in connection pool to " \
                       "process \"" MHD_HTTP_HEADER_AUTHORIZATION "\" " \
                       "header.\n"));
#endif /* HAVE_MESSAGES */
          return false;
        }
        memset (rq_auth, 0, sizeof (struct MHD_AuthRqHeader)
                + sizeof (struct MHD_RqDAuth));
        rq_auth->params.dauth = (struct MHD_RqDAuth *) (rq_auth + 1);

        if (h_len > i)
        {
          if (! parse_dauth_params (h + i, h_len - i, rq_auth->params.dauth))
          {
            rq_auth->auth_type = MHD_AUTHTYPE_INVALID;
            return false;
          }
        }

        rq_auth->auth_type = MHD_AUTHTYPE_DIGEST;
        return true;
      }
    }
  }
#endif /* DAUTH_SUPPORT */
#ifdef BAUTH_SUPPORT
  if (1)
  {
    static const struct _MHD_cstr_w_len scheme_token =
      _MHD_S_STR_W_LEN (_MHD_AUTH_BASIC_BASE);

    if ((scheme_token.len <= h_len) &&
        MHD_str_equal_caseless_bin_n_ (h, scheme_token.str, scheme_token.len))
    {
      i = scheme_token.len;
      /* RFC 7235 require only space after scheme token */
      if ( (h_len <= i) ||
           ((' ' == h[i]) || ('\t' == h[i])) ) /* Actually tab should NOT be allowed */
      { /* Matched Basic authorisation scheme */
        i++; /* Advance to the next char (even if it is beyond the end of the string) */

        rq_auth = (struct MHD_AuthRqHeader *)
                  MHD_connection_alloc_memory_ (c,
                                                sizeof (struct MHD_AuthRqHeader)
                                                + sizeof (struct MHD_RqBAuth));
        c->rq_auth = rq_auth;
        if (NULL == rq_auth)
        {
#ifdef HAVE_MESSAGES
          MHD_DLOG (c->daemon,
                    _ ("Failed to allocate memory in connection pool to " \
                       "process \"" MHD_HTTP_HEADER_AUTHORIZATION "\" " \
                       "header.\n"));
#endif /* HAVE_MESSAGES */
          return false;
        }
        memset (rq_auth, 0, sizeof (struct MHD_AuthRqHeader)
                + sizeof (struct MHD_RqBAuth));
        rq_auth->params.bauth = (struct MHD_RqBAuth *) (rq_auth + 1);

        if (h_len > i)
        {
          if (! parse_bauth_params (h + i, h_len - i, rq_auth->params.bauth))
          {
            rq_auth->auth_type = MHD_AUTHTYPE_INVALID;
            return false;
          }
        }

        rq_auth->auth_type = MHD_AUTHTYPE_BASIC;
        return true;
      }
    }
  }
#endif /* BAUTH_SUPPORT */

  if (NULL == rq_auth)
    rq_auth = (struct MHD_AuthRqHeader *)
              MHD_connection_alloc_memory_ (c,
                                            sizeof (struct MHD_AuthRqHeader));
  c->rq_auth = rq_auth;
  if (NULL != rq_auth)
  {
    memset (rq_auth, 0, sizeof(struct MHD_AuthRqHeader));
    rq_auth->auth_type = MHD_AUTHTYPE_INVALID;
  }
  return false;
}


/**
 * Return request's Authentication type and parameters.
 *
 * Function return result of parsing of the request's "Authorization" header or
 * returns cached parsing result if the header was already parsed for
 * the current request.
 * @param connection the connection to process
 * @return the pointer to structure with Authentication type and parameters,
 *         NULL if no memory in memory pool or if called too early (before
 *         header has been received).
 */
const struct MHD_AuthRqHeader *
MHD_get_auth_rq_params_ (struct MHD_Connection *connection)
{
  mhd_assert (MHD_CONNECTION_HEADERS_PROCESSED <= connection->state);

  if (NULL != connection->rq_auth)
    return connection->rq_auth;

  if (MHD_CONNECTION_HEADERS_PROCESSED > connection->state)
    return NULL;

  parse_auth_rq_header_ (connection);

  return connection->rq_auth;
}