~ubuntu-branches/ubuntu/saucy/lighttpd/saucy

« back to all changes in this revision

Viewing changes to src/http_auth.c

  • Committer: Package Import Robot
  • Author(s): Lorenzo De Liso
  • Date: 2012-12-06 17:54:59 UTC
  • mfrom: (6.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20121206175459-aq6vz5xa9fa202jw
Tags: 1.4.31-3ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: libgamin-dev rather than libfam-dev to fix startup warning.
  - debian/index.html: s/Debian/Ubuntu/g branding on the default page.
  - Added a UFW profile set:
    + debian/lighttpd.dirs: added etc/ufw/applications.d
    + debian/rules: install the ufw profile.
    + debian/control: Suggests on ufw.
  - Add lighttpd-dev package:
    + debian/control: Added lighttpd-dev package; Build-depends on
      automake, libtool
    + debian/lighttpd-dev.install: Added.
  - debian/rules: Add override_dh_installinit to set "defaults 91 09" to not
    start before apache2 but in the same runlevel with the same priority.
  - debian/patches/build-dev-package.patch: Updated
  - debian/lighttpd.conf: Comment 'use-ipv6.pl' by default, which causes
    failure to bind port in ipv4
* debian/index.html: corrected BTS Ubuntu link for lighttpd

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "server.h"
2
2
#include "log.h"
3
3
#include "http_auth.h"
4
 
#include "http_auth_digest.h"
5
4
#include "inet_ntop_cache.h"
6
5
#include "stream.h"
7
6
 
28
27
#include <unistd.h>
29
28
#include <ctype.h>
30
29
 
31
 
#ifdef USE_OPENSSL
32
 
# include <openssl/md5.h>
33
 
#else
34
 
# include "md5.h"
35
 
#endif
 
30
#include "md5.h"
 
31
 
 
32
#define HASHLEN 16
 
33
#define HASHHEXLEN 32
 
34
typedef unsigned char HASH[HASHLEN];
 
35
typedef char HASHHEX[HASHHEXLEN+1];
 
36
 
 
37
static void CvtHex(const HASH Bin, char Hex[33]) {
 
38
        unsigned short i;
 
39
 
 
40
        for (i = 0; i < 16; i++) {
 
41
                Hex[i*2] = int2hex((Bin[i] >> 4) & 0xf);
 
42
                Hex[i*2+1] = int2hex(Bin[i] & 0xf);
 
43
        }
 
44
        Hex[32] = '\0';
 
45
}
36
46
 
37
47
/**
38
48
 * the $apr1$ handling is taken from apache 1.3.x
429
439
 
430
440
static void to64(char *s, unsigned long v, int n)
431
441
{
432
 
    static unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
 
442
    static const unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
433
443
        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
434
444
 
435
445
    while (--n >= 0) {
449
459
    const char *sp, *ep;
450
460
    unsigned char final[APR_MD5_DIGESTSIZE];
451
461
    ssize_t sl, pl, i;
452
 
    MD5_CTX ctx, ctx1;
 
462
    li_MD5_CTX ctx, ctx1;
453
463
    unsigned long l;
454
464
 
455
465
    /*
481
491
    /*
482
492
     * 'Time to make the doughnuts..'
483
493
     */
484
 
    MD5_Init(&ctx);
 
494
    li_MD5_Init(&ctx);
485
495
 
486
496
    /*
487
497
     * The password first, since that is what is most unknown
488
498
     */
489
 
    MD5_Update(&ctx, pw, strlen(pw));
 
499
    li_MD5_Update(&ctx, pw, strlen(pw));
490
500
 
491
501
    /*
492
502
     * Then our magic string
493
503
     */
494
 
    MD5_Update(&ctx, APR1_ID, strlen(APR1_ID));
 
504
    li_MD5_Update(&ctx, APR1_ID, strlen(APR1_ID));
495
505
 
496
506
    /*
497
507
     * Then the raw salt
498
508
     */
499
 
    MD5_Update(&ctx, sp, sl);
 
509
    li_MD5_Update(&ctx, sp, sl);
500
510
 
501
511
    /*
502
512
     * Then just as many characters of the MD5(pw, salt, pw)
503
513
     */
504
 
    MD5_Init(&ctx1);
505
 
    MD5_Update(&ctx1, pw, strlen(pw));
506
 
    MD5_Update(&ctx1, sp, sl);
507
 
    MD5_Update(&ctx1, pw, strlen(pw));
508
 
    MD5_Final(final, &ctx1);
 
514
    li_MD5_Init(&ctx1);
 
515
    li_MD5_Update(&ctx1, pw, strlen(pw));
 
516
    li_MD5_Update(&ctx1, sp, sl);
 
517
    li_MD5_Update(&ctx1, pw, strlen(pw));
 
518
    li_MD5_Final(final, &ctx1);
509
519
    for (pl = strlen(pw); pl > 0; pl -= APR_MD5_DIGESTSIZE) {
510
 
        MD5_Update(&ctx, final,
 
520
        li_MD5_Update(&ctx, final,
511
521
                      (pl > APR_MD5_DIGESTSIZE) ? APR_MD5_DIGESTSIZE : pl);
512
522
    }
513
523
 
521
531
     */
522
532
    for (i = strlen(pw); i != 0; i >>= 1) {
523
533
        if (i & 1) {
524
 
            MD5_Update(&ctx, final, 1);
 
534
            li_MD5_Update(&ctx, final, 1);
525
535
        }
526
536
        else {
527
 
            MD5_Update(&ctx, pw, 1);
 
537
            li_MD5_Update(&ctx, pw, 1);
528
538
        }
529
539
    }
530
540
 
536
546
    strncat(passwd, sp, sl);
537
547
    strcat(passwd, "$");
538
548
 
539
 
    MD5_Final(final, &ctx);
 
549
    li_MD5_Final(final, &ctx);
540
550
 
541
551
    /*
542
552
     * And now, just to make sure things don't run too fast..
544
554
     * need 30 seconds to build a 1000 entry dictionary...
545
555
     */
546
556
    for (i = 0; i < 1000; i++) {
547
 
        MD5_Init(&ctx1);
 
557
        li_MD5_Init(&ctx1);
548
558
        if (i & 1) {
549
 
            MD5_Update(&ctx1, pw, strlen(pw));
 
559
            li_MD5_Update(&ctx1, pw, strlen(pw));
550
560
        }
551
561
        else {
552
 
            MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE);
 
562
            li_MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE);
553
563
        }
554
564
        if (i % 3) {
555
 
            MD5_Update(&ctx1, sp, sl);
 
565
            li_MD5_Update(&ctx1, sp, sl);
556
566
        }
557
567
 
558
568
        if (i % 7) {
559
 
            MD5_Update(&ctx1, pw, strlen(pw));
 
569
            li_MD5_Update(&ctx1, pw, strlen(pw));
560
570
        }
561
571
 
562
572
        if (i & 1) {
563
 
            MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE);
 
573
            li_MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE);
564
574
        }
565
575
        else {
566
 
            MD5_Update(&ctx1, pw, strlen(pw));
 
576
            li_MD5_Update(&ctx1, pw, strlen(pw));
567
577
        }
568
 
        MD5_Final(final,&ctx1);
 
578
        li_MD5_Final(final,&ctx1);
569
579
    }
570
580
 
571
581
    p = passwd + strlen(passwd);
608
618
                 * user:realm:md5(user:realm:password)
609
619
                 */
610
620
 
611
 
                MD5_CTX Md5Ctx;
 
621
                li_MD5_CTX Md5Ctx;
612
622
                HASH HA1;
613
623
                char a1[256];
614
624
 
615
 
                MD5_Init(&Md5Ctx);
616
 
                MD5_Update(&Md5Ctx, (unsigned char *)username->ptr, username->used - 1);
617
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
618
 
                MD5_Update(&Md5Ctx, (unsigned char *)realm->ptr, realm->used - 1);
619
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
620
 
                MD5_Update(&Md5Ctx, (unsigned char *)pw, strlen(pw));
621
 
                MD5_Final(HA1, &Md5Ctx);
 
625
                li_MD5_Init(&Md5Ctx);
 
626
                li_MD5_Update(&Md5Ctx, (unsigned char *)username->ptr, username->used - 1);
 
627
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
628
                li_MD5_Update(&Md5Ctx, (unsigned char *)realm->ptr, realm->used - 1);
 
629
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
630
                li_MD5_Update(&Md5Ctx, (unsigned char *)pw, strlen(pw));
 
631
                li_MD5_Final(HA1, &Md5Ctx);
622
632
 
623
633
                CvtHex(HA1, a1);
624
634
 
924
934
        int i;
925
935
        buffer *password, *b, *username_buf, *realm_buf;
926
936
 
927
 
        MD5_CTX Md5Ctx;
 
937
        li_MD5_CTX Md5Ctx;
928
938
        HASH HA1;
929
939
        HASH HA2;
930
940
        HASH RespHash;
1006
1016
                log_error_write(srv, __FILE__, __LINE__, "ss", "realm", realm);
1007
1017
                log_error_write(srv, __FILE__, __LINE__, "ss", "nonce", nonce);
1008
1018
                log_error_write(srv, __FILE__, __LINE__, "ss", "uri", uri);
1009
 
                log_error_write(srv, __FILE__, __LINE__, "ss", "algorigthm", algorithm);
 
1019
                log_error_write(srv, __FILE__, __LINE__, "ss", "algorithm", algorithm);
1010
1020
                log_error_write(srv, __FILE__, __LINE__, "ss", "qop", qop);
1011
1021
                log_error_write(srv, __FILE__, __LINE__, "ss", "cnonce", cnonce);
1012
1022
                log_error_write(srv, __FILE__, __LINE__, "ss", "nc", nc);
1061
1071
 
1062
1072
        if (p->conf.auth_backend == AUTH_BACKEND_PLAIN) {
1063
1073
                /* generate password from plain-text */
1064
 
                MD5_Init(&Md5Ctx);
1065
 
                MD5_Update(&Md5Ctx, (unsigned char *)username, strlen(username));
1066
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1067
 
                MD5_Update(&Md5Ctx, (unsigned char *)realm, strlen(realm));
1068
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1069
 
                MD5_Update(&Md5Ctx, (unsigned char *)password->ptr, password->used - 1);
1070
 
                MD5_Final(HA1, &Md5Ctx);
 
1074
                li_MD5_Init(&Md5Ctx);
 
1075
                li_MD5_Update(&Md5Ctx, (unsigned char *)username, strlen(username));
 
1076
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1077
                li_MD5_Update(&Md5Ctx, (unsigned char *)realm, strlen(realm));
 
1078
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1079
                li_MD5_Update(&Md5Ctx, (unsigned char *)password->ptr, password->used - 1);
 
1080
                li_MD5_Final(HA1, &Md5Ctx);
1071
1081
        } else if (p->conf.auth_backend == AUTH_BACKEND_HTDIGEST) {
1072
1082
                /* HA1 */
1073
1083
                /* transform the 32-byte-hex-md5 to a 16-byte-md5 */
1084
1094
 
1085
1095
        if (algorithm &&
1086
1096
            strcasecmp(algorithm, "md5-sess") == 0) {
1087
 
                MD5_Init(&Md5Ctx);
1088
 
                MD5_Update(&Md5Ctx, (unsigned char *)HA1, 16);
1089
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1090
 
                MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce));
1091
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1092
 
                MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce));
1093
 
                MD5_Final(HA1, &Md5Ctx);
 
1097
                li_MD5_Init(&Md5Ctx);
 
1098
                /* Errata ID 1649: http://www.rfc-editor.org/errata_search.php?rfc=2617 */
 
1099
                CvtHex(HA1, a1);
 
1100
                li_MD5_Update(&Md5Ctx, (unsigned char *)a1, 32);
 
1101
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1102
                li_MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce));
 
1103
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1104
                li_MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce));
 
1105
                li_MD5_Final(HA1, &Md5Ctx);
1094
1106
        }
1095
1107
 
1096
1108
        CvtHex(HA1, a1);
1097
1109
 
1098
1110
        /* calculate H(A2) */
1099
 
        MD5_Init(&Md5Ctx);
1100
 
        MD5_Update(&Md5Ctx, (unsigned char *)m, strlen(m));
1101
 
        MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1102
 
        MD5_Update(&Md5Ctx, (unsigned char *)uri, strlen(uri));
 
1111
        li_MD5_Init(&Md5Ctx);
 
1112
        li_MD5_Update(&Md5Ctx, (unsigned char *)m, strlen(m));
 
1113
        li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1114
        li_MD5_Update(&Md5Ctx, (unsigned char *)uri, strlen(uri));
1103
1115
        if (qop && strcasecmp(qop, "auth-int") == 0) {
1104
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1105
 
                MD5_Update(&Md5Ctx, (unsigned char *)"", HASHHEXLEN);
 
1116
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1117
                li_MD5_Update(&Md5Ctx, (unsigned char *)"", HASHHEXLEN);
1106
1118
        }
1107
 
        MD5_Final(HA2, &Md5Ctx);
 
1119
        li_MD5_Final(HA2, &Md5Ctx);
1108
1120
        CvtHex(HA2, HA2Hex);
1109
1121
 
1110
1122
        /* calculate response */
1111
 
        MD5_Init(&Md5Ctx);
1112
 
        MD5_Update(&Md5Ctx, (unsigned char *)a1, HASHHEXLEN);
1113
 
        MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1114
 
        MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce));
1115
 
        MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1123
        li_MD5_Init(&Md5Ctx);
 
1124
        li_MD5_Update(&Md5Ctx, (unsigned char *)a1, HASHHEXLEN);
 
1125
        li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1126
        li_MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce));
 
1127
        li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1116
1128
        if (qop && *qop) {
1117
 
                MD5_Update(&Md5Ctx, (unsigned char *)nc, strlen(nc));
1118
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1119
 
                MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce));
1120
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1121
 
                MD5_Update(&Md5Ctx, (unsigned char *)qop, strlen(qop));
1122
 
                MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1129
                li_MD5_Update(&Md5Ctx, (unsigned char *)nc, strlen(nc));
 
1130
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1131
                li_MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce));
 
1132
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
 
1133
                li_MD5_Update(&Md5Ctx, (unsigned char *)qop, strlen(qop));
 
1134
                li_MD5_Update(&Md5Ctx, (unsigned char *)":", 1);
1123
1135
        };
1124
 
        MD5_Update(&Md5Ctx, (unsigned char *)HA2Hex, HASHHEXLEN);
1125
 
        MD5_Final(RespHash, &Md5Ctx);
 
1136
        li_MD5_Update(&Md5Ctx, (unsigned char *)HA2Hex, HASHHEXLEN);
 
1137
        li_MD5_Final(RespHash, &Md5Ctx);
1126
1138
        CvtHex(RespHash, a2);
1127
1139
 
1128
1140
        if (0 != strcmp(a2, respons)) {
1165
1177
 
1166
1178
int http_auth_digest_generate_nonce(server *srv, mod_auth_plugin_data *p, buffer *fn, char out[33]) {
1167
1179
        HASH h;
1168
 
        MD5_CTX Md5Ctx;
 
1180
        li_MD5_CTX Md5Ctx;
1169
1181
        char hh[32];
1170
1182
 
1171
1183
        UNUSED(p);
1172
1184
 
1173
1185
        /* generate shared-secret */
1174
 
        MD5_Init(&Md5Ctx);
1175
 
        MD5_Update(&Md5Ctx, (unsigned char *)fn->ptr, fn->used - 1);
1176
 
        MD5_Update(&Md5Ctx, (unsigned char *)"+", 1);
 
1186
        li_MD5_Init(&Md5Ctx);
 
1187
        li_MD5_Update(&Md5Ctx, (unsigned char *)fn->ptr, fn->used - 1);
 
1188
        li_MD5_Update(&Md5Ctx, (unsigned char *)"+", 1);
1177
1189
 
1178
1190
        /* we assume sizeof(time_t) == 4 here, but if not it ain't a problem at all */
1179
1191
        LI_ltostr(hh, srv->cur_ts);
1180
 
        MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh));
1181
 
        MD5_Update(&Md5Ctx, (unsigned char *)srv->entropy, sizeof(srv->entropy));
 
1192
        li_MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh));
 
1193
        li_MD5_Update(&Md5Ctx, (unsigned char *)srv->entropy, sizeof(srv->entropy));
1182
1194
        LI_ltostr(hh, rand());
1183
 
        MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh));
 
1195
        li_MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh));
1184
1196
 
1185
 
        MD5_Final(h, &Md5Ctx);
 
1197
        li_MD5_Final(h, &Md5Ctx);
1186
1198
 
1187
1199
        CvtHex(h, out);
1188
1200