summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2013-07-08 13:30:22 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2013-07-08 13:34:34 (GMT)
commit08a7915aaf08e9d2187fd3f26e0326673d4c8eb1 (patch) (unidiff)
tree7d9f02b59945ae7577e79c3cbfe5182b3a22e4b8
parent4e2b6995088963e287de5dcc507a5ab14a7134b1 (diff)
downloadvmod-basicauth-08a7915aaf08e9d2187fd3f26e0326673d4c8eb1.tar.gz
vmod-basicauth-08a7915aaf08e9d2187fd3f26e0326673d4c8eb1.tar.bz2
Implement SHA1 passwords.
* src/sha1.c: New file. * src/sha1.h: New file. * src/Makefile.am: Add new files. * src/vmod_basicauth.c (base64_decode): Fix bound checking. (sha1_match): New function. (match_tab): Add SHA1 method. * tests/test03.vtc: New test. * tests/Makefile.am: Add new file. * tests/htpasswd: Add SHA1 password. * README: Update. * src/vmod_basicauth.3: Update.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--README4
-rw-r--r--src/Makefile.am2
-rw-r--r--src/sha1.c418
-rw-r--r--src/sha1.h98
-rw-r--r--src/vmod_basicauth.35
-rw-r--r--src/vmod_basicauth.c27
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/htpasswd1
-rw-r--r--tests/test03.vtc23
9 files changed, 575 insertions, 6 deletions
diff --git a/README b/README
index 039699f..c6dd102 100644
--- a/README
+++ b/README
@@ -1,8 +1,8 @@
1* Overview 1* Overview
2 2
3This module implements basic HTTP authentication against the password file 3This module implements basic HTTP authentication against the password file
4created with the htpasswd(1) utility. Three kinds of password hashes are 4created with the htpasswd(1) utility. The following password hashes are
5supported: Apache MD5, crypt(3), and plaintext. 5supported: Apache MD5, crypt(3), SHA1, and plaintext.
6 6
7* Example 7* Example
8 8
diff --git a/src/Makefile.am b/src/Makefile.am
index 92f15ca..e4fdb51 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -11,6 +11,8 @@ libvmod_basicauth_la_LIBADD=-lcrypt
11libvmod_basicauth_la_SOURCES = \ 11libvmod_basicauth_la_SOURCES = \
12 basicauth.h\ 12 basicauth.h\
13 md5.c\ 13 md5.c\
14 sha1.c\
15 sha1.h\
14 vcc_if.c \ 16 vcc_if.c \
15 vcc_if.h \ 17 vcc_if.h \
16 vmod_basicauth.c 18 vmod_basicauth.c
diff --git a/src/sha1.c b/src/sha1.c
new file mode 100644
index 0000000..a4380cb
--- a/dev/null
+++ b/src/sha1.c
@@ -0,0 +1,418 @@
1/* sha1.c - Functions to compute SHA1 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-1.
3
4 Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006 Free Software
5 Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program 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
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21/* Written by Scott G. Miller
22 Credits:
23 Robert Klep <robert@ilse.nl> -- Expansion function fix
24*/
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30#include <sha1.h>
31
32#include <stddef.h>
33#include <string.h>
34
35#if USE_UNLOCKED_IO
36# include "unlocked-io.h"
37#endif
38
39#ifdef WORDS_BIGENDIAN
40# define SWAP(n) (n)
41#else
42# define SWAP(n) \
43 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
44#endif
45
46#define BLOCKSIZE 4096
47#if BLOCKSIZE % 64 != 0
48# error "invalid BLOCKSIZE"
49#endif
50
51/* This array contains the bytes used to pad the buffer to the next
52 64-byte boundary. (RFC 1321, 3.1: Step 1) */
53static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
54
55
56/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
57 initialize it to the start constants of the SHA1 algorithm. This
58 must be called before using hash in the call to sha1_hash. */
59void
60sha1_init_ctx (struct sha1_ctx *ctx)
61{
62 ctx->A = 0x67452301;
63 ctx->B = 0xefcdab89;
64 ctx->C = 0x98badcfe;
65 ctx->D = 0x10325476;
66 ctx->E = 0xc3d2e1f0;
67
68 ctx->total[0] = ctx->total[1] = 0;
69 ctx->buflen = 0;
70}
71
72/* Put result from CTX in first 20 bytes following RESBUF. The result
73 must be in little endian byte order.
74
75 IMPORTANT: On some systems it is required that RESBUF is correctly
76 aligned for a 32-bit value. */
77void *
78sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
79{
80 ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
81 ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
82 ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
83 ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
84 ((uint32_t *) resbuf)[4] = SWAP (ctx->E);
85
86 return resbuf;
87}
88
89/* Process the remaining bytes in the internal buffer and the usual
90 prolog according to the standard and write the result to RESBUF.
91
92 IMPORTANT: On some systems it is required that RESBUF is correctly
93 aligned for a 32-bit value. */
94void *
95sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
96{
97 /* Take yet unprocessed bytes into account. */
98 uint32_t bytes = ctx->buflen;
99 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
100
101 /* Now count remaining bytes. */
102 ctx->total[0] += bytes;
103 if (ctx->total[0] < bytes)
104 ++ctx->total[1];
105
106 /* Put the 64-bit file length in *bits* at the end of the buffer. */
107 ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
108 ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
109
110 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
111
112 /* Process last bytes. */
113 sha1_process_block (ctx->buffer, size * 4, ctx);
114
115 return sha1_read_ctx (ctx, resbuf);
116}
117
118/* Compute SHA1 message digest for bytes read from STREAM. The
119 resulting message digest number will be written into the 16 bytes
120 beginning at RESBLOCK. */
121int
122sha1_stream (FILE *stream, void *resblock)
123{
124 struct sha1_ctx ctx;
125 char buffer[BLOCKSIZE + 72];
126 size_t sum;
127
128 /* Initialize the computation context. */
129 sha1_init_ctx (&ctx);
130
131 /* Iterate over full file contents. */
132 while (1)
133 {
134 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
135 computation function processes the whole buffer so that with the
136 next round of the loop another block can be read. */
137 size_t n;
138 sum = 0;
139
140 /* Read block. Take care for partial reads. */
141 while (1)
142 {
143 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
144
145 sum += n;
146
147 if (sum == BLOCKSIZE)
148 break;
149
150 if (n == 0)
151 {
152 /* Check for the error flag IFF N == 0, so that we don't
153 exit the loop after a partial read due to e.g., EAGAIN
154 or EWOULDBLOCK. */
155 if (ferror (stream))
156 return 1;
157 goto process_partial_block;
158 }
159
160 /* We've read at least one byte, so ignore errors. But always
161 check for EOF, since feof may be true even though N > 0.
162 Otherwise, we could end up calling fread after EOF. */
163 if (feof (stream))
164 goto process_partial_block;
165 }
166
167 /* Process buffer with BLOCKSIZE bytes. Note that
168 BLOCKSIZE % 64 == 0
169 */
170 sha1_process_block (buffer, BLOCKSIZE, &ctx);
171 }
172
173 process_partial_block:;
174
175 /* Process any remaining bytes. */
176 if (sum > 0)
177 sha1_process_bytes (buffer, sum, &ctx);
178
179 /* Construct result in desired memory. */
180 sha1_finish_ctx (&ctx, resblock);
181 return 0;
182}
183
184/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
185 result is always in little endian byte order, so that a byte-wise
186 output yields to the wanted ASCII representation of the message
187 digest. */
188void *
189sha1_buffer (const char *buffer, size_t len, void *resblock)
190{
191 struct sha1_ctx ctx;
192
193 /* Initialize the computation context. */
194 sha1_init_ctx (&ctx);
195
196 /* Process whole buffer but last len % 64 bytes. */
197 sha1_process_bytes (buffer, len, &ctx);
198
199 /* Put result in desired memory area. */
200 return sha1_finish_ctx (&ctx, resblock);
201}
202
203void
204sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
205{
206 /* When we already have some bits in our internal buffer concatenate
207 both inputs first. */
208 if (ctx->buflen != 0)
209 {
210 size_t left_over = ctx->buflen;
211 size_t add = 128 - left_over > len ? len : 128 - left_over;
212
213 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
214 ctx->buflen += add;
215
216 if (ctx->buflen > 64)
217 {
218 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
219
220 ctx->buflen &= 63;
221 /* The regions in the following copy operation cannot overlap. */
222 memcpy (ctx->buffer,
223 &((char *) ctx->buffer)[(left_over + add) & ~63],
224 ctx->buflen);
225 }
226
227 buffer = (const char *) buffer + add;
228 len -= add;
229 }
230
231 /* Process available complete blocks. */
232 if (len >= 64)
233 {
234#if !_STRING_ARCH_unaligned
235# define alignof(type) offsetof (struct { char c; type x; }, x)
236# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
237 if (UNALIGNED_P (buffer))
238 while (len > 64)
239 {
240 sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
241 buffer = (const char *) buffer + 64;
242 len -= 64;
243 }
244 else
245#endif
246 {
247 sha1_process_block (buffer, len & ~63, ctx);
248 buffer = (const char *) buffer + (len & ~63);
249 len &= 63;
250 }
251 }
252
253 /* Move remaining bytes in internal buffer. */
254 if (len > 0)
255 {
256 size_t left_over = ctx->buflen;
257
258 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
259 left_over += len;
260 if (left_over >= 64)
261 {
262 sha1_process_block (ctx->buffer, 64, ctx);
263 left_over -= 64;
264 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
265 }
266 ctx->buflen = left_over;
267 }
268}
269
270/* --- Code below is the primary difference between md5.c and sha1.c --- */
271
272/* SHA1 round constants */
273#define K1 0x5a827999
274#define K2 0x6ed9eba1
275#define K3 0x8f1bbcdc
276#define K4 0xca62c1d6
277
278/* Round functions. Note that F2 is the same as F4. */
279#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
280#define F2(B,C,D) (B ^ C ^ D)
281#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
282#define F4(B,C,D) (B ^ C ^ D)
283
284/* Process LEN bytes of BUFFER, accumulating context into CTX.
285 It is assumed that LEN % 64 == 0.
286 Most of this code comes from GnuPG's cipher/sha1.c. */
287
288void
289sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
290{
291 const uint32_t *words = buffer;
292 size_t nwords = len / sizeof (uint32_t);
293 const uint32_t *endp = words + nwords;
294 uint32_t x[16];
295 uint32_t a = ctx->A;
296 uint32_t b = ctx->B;
297 uint32_t c = ctx->C;
298 uint32_t d = ctx->D;
299 uint32_t e = ctx->E;
300
301 /* First increment the byte count. RFC 1321 specifies the possible
302 length of the file up to 2^64 bits. Here we only compute the
303 number of bytes. Do a double word increment. */
304 ctx->total[0] += len;
305 if (ctx->total[0] < len)
306 ++ctx->total[1];
307
308#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
309
310#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
311 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
312 , (x[I&0x0f] = rol(tm, 1)) )
313
314#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
315 + F( B, C, D ) \
316 + K \
317 + M; \
318 B = rol( B, 30 ); \
319 } while(0)
320
321 while (words < endp)
322 {
323 uint32_t tm;
324 int t;
325 for (t = 0; t < 16; t++)
326 {
327 x[t] = SWAP (*words);
328 words++;
329 }
330
331 R( a, b, c, d, e, F1, K1, x[ 0] );
332 R( e, a, b, c, d, F1, K1, x[ 1] );
333 R( d, e, a, b, c, F1, K1, x[ 2] );
334 R( c, d, e, a, b, F1, K1, x[ 3] );
335 R( b, c, d, e, a, F1, K1, x[ 4] );
336 R( a, b, c, d, e, F1, K1, x[ 5] );
337 R( e, a, b, c, d, F1, K1, x[ 6] );
338 R( d, e, a, b, c, F1, K1, x[ 7] );
339 R( c, d, e, a, b, F1, K1, x[ 8] );
340 R( b, c, d, e, a, F1, K1, x[ 9] );
341 R( a, b, c, d, e, F1, K1, x[10] );
342 R( e, a, b, c, d, F1, K1, x[11] );
343 R( d, e, a, b, c, F1, K1, x[12] );
344 R( c, d, e, a, b, F1, K1, x[13] );
345 R( b, c, d, e, a, F1, K1, x[14] );
346 R( a, b, c, d, e, F1, K1, x[15] );
347 R( e, a, b, c, d, F1, K1, M(16) );
348 R( d, e, a, b, c, F1, K1, M(17) );
349 R( c, d, e, a, b, F1, K1, M(18) );
350 R( b, c, d, e, a, F1, K1, M(19) );
351 R( a, b, c, d, e, F2, K2, M(20) );
352 R( e, a, b, c, d, F2, K2, M(21) );
353 R( d, e, a, b, c, F2, K2, M(22) );
354 R( c, d, e, a, b, F2, K2, M(23) );
355 R( b, c, d, e, a, F2, K2, M(24) );
356 R( a, b, c, d, e, F2, K2, M(25) );
357 R( e, a, b, c, d, F2, K2, M(26) );
358 R( d, e, a, b, c, F2, K2, M(27) );
359 R( c, d, e, a, b, F2, K2, M(28) );
360 R( b, c, d, e, a, F2, K2, M(29) );
361 R( a, b, c, d, e, F2, K2, M(30) );
362 R( e, a, b, c, d, F2, K2, M(31) );
363 R( d, e, a, b, c, F2, K2, M(32) );
364 R( c, d, e, a, b, F2, K2, M(33) );
365 R( b, c, d, e, a, F2, K2, M(34) );
366 R( a, b, c, d, e, F2, K2, M(35) );
367 R( e, a, b, c, d, F2, K2, M(36) );
368 R( d, e, a, b, c, F2, K2, M(37) );
369 R( c, d, e, a, b, F2, K2, M(38) );
370 R( b, c, d, e, a, F2, K2, M(39) );
371 R( a, b, c, d, e, F3, K3, M(40) );
372 R( e, a, b, c, d, F3, K3, M(41) );
373 R( d, e, a, b, c, F3, K3, M(42) );
374 R( c, d, e, a, b, F3, K3, M(43) );
375 R( b, c, d, e, a, F3, K3, M(44) );
376 R( a, b, c, d, e, F3, K3, M(45) );
377 R( e, a, b, c, d, F3, K3, M(46) );
378 R( d, e, a, b, c, F3, K3, M(47) );
379 R( c, d, e, a, b, F3, K3, M(48) );
380 R( b, c, d, e, a, F3, K3, M(49) );
381 R( a, b, c, d, e, F3, K3, M(50) );
382 R( e, a, b, c, d, F3, K3, M(51) );
383 R( d, e, a, b, c, F3, K3, M(52) );
384 R( c, d, e, a, b, F3, K3, M(53) );
385 R( b, c, d, e, a, F3, K3, M(54) );
386 R( a, b, c, d, e, F3, K3, M(55) );
387 R( e, a, b, c, d, F3, K3, M(56) );
388 R( d, e, a, b, c, F3, K3, M(57) );
389 R( c, d, e, a, b, F3, K3, M(58) );
390 R( b, c, d, e, a, F3, K3, M(59) );
391 R( a, b, c, d, e, F4, K4, M(60) );
392 R( e, a, b, c, d, F4, K4, M(61) );
393 R( d, e, a, b, c, F4, K4, M(62) );
394 R( c, d, e, a, b, F4, K4, M(63) );
395 R( b, c, d, e, a, F4, K4, M(64) );
396 R( a, b, c, d, e, F4, K4, M(65) );
397 R( e, a, b, c, d, F4, K4, M(66) );
398 R( d, e, a, b, c, F4, K4, M(67) );
399 R( c, d, e, a, b, F4, K4, M(68) );
400 R( b, c, d, e, a, F4, K4, M(69) );
401 R( a, b, c, d, e, F4, K4, M(70) );
402 R( e, a, b, c, d, F4, K4, M(71) );
403 R( d, e, a, b, c, F4, K4, M(72) );
404 R( c, d, e, a, b, F4, K4, M(73) );
405 R( b, c, d, e, a, F4, K4, M(74) );
406 R( a, b, c, d, e, F4, K4, M(75) );
407 R( e, a, b, c, d, F4, K4, M(76) );
408 R( d, e, a, b, c, F4, K4, M(77) );
409 R( c, d, e, a, b, F4, K4, M(78) );
410 R( b, c, d, e, a, F4, K4, M(79) );
411
412 a = ctx->A += a;
413 b = ctx->B += b;
414 c = ctx->C += c;
415 d = ctx->D += d;
416 e = ctx->E += e;
417 }
418}
diff --git a/src/sha1.h b/src/sha1.h
new file mode 100644
index 0000000..0a4d3b9
--- a/dev/null
+++ b/src/sha1.h
@@ -0,0 +1,98 @@
1/* Declarations of functions and data types used for SHA1 sum
2 library functions.
3 Copyright (C) 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#ifndef _ECLAT_SHA1_H
20# define _ECLAT_SHA1_H 1
21
22# include <stdio.h>
23# include <stdint.h>
24
25/* Rename to avoid name clashes */
26# define sha1_ctx eclat_sha1_ctx
27# define sha1_init_ctx eclat_sha1_init_ctx
28# define sha1_process_block eclat_sha1_process_block
29# define sha1_process_bytes eclat_sha1_process_bytes
30# define sha1_finish_ctx eclat_sha1_finish_ctx
31# define sha1_read_ctx eclat_sha1_read_ctx
32# define sha1_stream eclat_sha1_stream
33# define sha1_buffer eclat_sha1_buffer
34# define sha1_ctx eclat_sha1_ctx
35
36/* Structure to save state of computation between the single steps. */
37struct sha1_ctx
38{
39 uint32_t A;
40 uint32_t B;
41 uint32_t C;
42 uint32_t D;
43 uint32_t E;
44
45 uint32_t total[2];
46 uint32_t buflen;
47 uint32_t buffer[32];
48};
49
50
51/* Initialize structure containing state of computation. */
52extern void sha1_init_ctx (struct sha1_ctx *ctx);
53
54/* Starting with the result of former calls of this function (or the
55 initialization function update the context for the next LEN bytes
56 starting at BUFFER.
57 It is necessary that LEN is a multiple of 64!!! */
58extern void sha1_process_block (const void *buffer, size_t len,
59 struct sha1_ctx *ctx);
60
61/* Starting with the result of former calls of this function (or the
62 initialization function update the context for the next LEN bytes
63 starting at BUFFER.
64 It is NOT required that LEN is a multiple of 64. */
65extern void sha1_process_bytes (const void *buffer, size_t len,
66 struct sha1_ctx *ctx);
67
68/* Process the remaining bytes in the buffer and put result from CTX
69 in first 20 bytes following RESBUF. The result is always in little
70 endian byte order, so that a byte-wise output yields to the wanted
71 ASCII representation of the message digest.
72
73 IMPORTANT: On some systems it is required that RESBUF be correctly
74 aligned for a 32 bits value. */
75extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
76
77
78/* Put result from CTX in first 20 bytes following RESBUF. The result is
79 always in little endian byte order, so that a byte-wise output yields
80 to the wanted ASCII representation of the message digest.
81
82 IMPORTANT: On some systems it is required that RESBUF is correctly
83 aligned for a 32 bits value. */
84extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
85
86
87/* Compute SHA1 message digest for bytes read from STREAM. The
88 resulting message digest number will be written into the 20 bytes
89 beginning at RESBLOCK. */
90extern int sha1_stream (FILE *stream, void *resblock);
91
92/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
93 result is always in little endian byte order, so that a byte-wise
94 output yields to the wanted ASCII representation of the message
95 digest. */
96extern void *sha1_buffer (const char *buffer, size_t len, void *resblock);
97
98#endif
diff --git a/src/vmod_basicauth.3 b/src/vmod_basicauth.3
index cde7c2a..d7c8934 100644
--- a/src/vmod_basicauth.3
+++ b/src/vmod_basicauth.3
@@ -13,7 +13,7 @@
13.\" 13.\"
14.\" You should have received a copy of the GNU General Public License 14.\" You should have received a copy of the GNU General Public License
15.\" along with libvmod_basicauth. If not, see <http://www.gnu.org/licenses/>. 15.\" along with libvmod_basicauth. If not, see <http://www.gnu.org/licenses/>.
16.TH VMOD_BASICAUTH 1 "July 7, 2013" "VMOD_BASICAUTH" "User Reference" 16.TH VMOD_BASICAUTH 1 "July 8, 2013" "VMOD_BASICAUTH" "User Reference"
17.SH NAME 17.SH NAME
18vmod_basicauth \- Basic HTTP authentication 18vmod_basicauth \- Basic HTTP authentication
19.SH SYNOPSIS 19.SH SYNOPSIS
@@ -26,9 +26,10 @@ created using the
26.BR htpasswd (1) 26.BR htpasswd (1)
27command. 27command.
28.PP 28.PP
29Three kinds of password hashes are supported: 29Four kinds of password hashes are supported:
30Apache MD5 (\fB$apr1$\fR), 30Apache MD5 (\fB$apr1$\fR),
31.BR crypt (3), 31.BR crypt (3),
32.BR SHA1 ,
32and plaintext. 33and plaintext.
33.SH FUNCTIONS 34.SH FUNCTIONS
34.SS match 35.SS match
diff --git a/src/vmod_basicauth.c b/src/vmod_basicauth.c
index 4af9569..0e66290 100644
--- a/src/vmod_basicauth.c
+++ b/src/vmod_basicauth.c
@@ -28,6 +28,7 @@
28#include "vcc_if.h" 28#include "vcc_if.h"
29 29
30#include "basicauth.h" 30#include "basicauth.h"
31#include "sha1.h"
31 32
32static int b64val[128] = { 33static int b64val[128] = {
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -45,10 +46,11 @@ base64_decode(const unsigned char *input, size_t input_len,
45 unsigned char *output, size_t output_len) 46 unsigned char *output, size_t output_len)
46{ 47{
47 unsigned char *out = output; 48 unsigned char *out = output;
48#define AC(c) do { if (--output_len == 0) return -1; *out++ = (c); } while (0) 49#define AC(c) do { if (output_len-- == 0) return -1; *out++ = (c); } while (0)
49 50
50 if (!out) 51 if (!out)
51 return -1; 52 return -1;
53
52 do { 54 do {
53 if (input[0] > 127 || b64val[input[0]] == -1 || 55 if (input[0] > 127 || b64val[input[0]] == -1 ||
54 input[1] > 127 || b64val[input[1]] == -1 || 56 input[1] > 127 || b64val[input[1]] == -1 ||
@@ -110,6 +112,28 @@ apr_match(const char *pass, const char *hash, struct priv_data *pd)
110 return strcmp(apr_md5_encode(pass, hash, buf, sizeof(buf)), hash); 112 return strcmp(apr_md5_encode(pass, hash, buf, sizeof(buf)), hash);
111} 113}
112 114
115#define SHA1_DIGEST_SIZE 20
116
117static int
118sha1_match(const char *pass, const char *hash, struct priv_data *pd)
119{
120 char hashbuf[SHA1_DIGEST_SIZE], resbuf[SHA1_DIGEST_SIZE];
121 int n;
122
123 hash += 5; /* Skip past {SHA} */
124 n = base64_decode(hash, strlen(hash), hashbuf, sizeof(hashbuf));
125 if (n < 0) {
126 syslog(LOG_AUTHPRIV|LOG_ERR, "cannot decode %s", hash);
127 return 1;
128 }
129 if (n != SHA1_DIGEST_SIZE) {
130 syslog(LOG_AUTHPRIV|LOG_ERR, "bad hash length: %s %d", hash, n);
131 return 1;
132 }
133 sha1_buffer(pass, strlen(pass), resbuf);
134
135 return memcmp(resbuf, hashbuf, SHA1_DIGEST_SIZE);
136}
113 137
114/* Matcher table */ 138/* Matcher table */
115struct matcher { 139struct matcher {
@@ -121,6 +145,7 @@ struct matcher {
121static struct matcher match_tab[] = { 145static struct matcher match_tab[] = {
122#define S(s) #s, sizeof(#s)-1 146#define S(s) #s, sizeof(#s)-1
123 { S($apr1$), apr_match }, 147 { S($apr1$), apr_match },
148 { S({SHA}), sha1_match },
124 { "", 0, crypt_match }, 149 { "", 0, crypt_match },
125 { "", 0, plain_match }, 150 { "", 0, plain_match },
126 { NULL } 151 { NULL }
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4e4347b..4c32621 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,7 +1,8 @@
1VMOD_TESTS = \ 1VMOD_TESTS = \
2 test00.vtc\ 2 test00.vtc\
3 test01.vtc\ 3 test01.vtc\
4 test02.vtc 4 test02.vtc\
5 test03.vtc
5 6
6EXTRA_DIST=$(VMOD_TESTS) htpasswd 7EXTRA_DIST=$(VMOD_TESTS) htpasswd
7 8
diff --git a/tests/htpasswd b/tests/htpasswd
index 5802542..918e141 100644
--- a/tests/htpasswd
+++ b/tests/htpasswd
@@ -1,3 +1,4 @@
1method-md5:$apr1$VMppzIb7$PyrBTfSWt4hGZhOG2WIae1 1method-md5:$apr1$VMppzIb7$PyrBTfSWt4hGZhOG2WIae1
2method-crypt:.GSZ.lgnfLS1. 2method-crypt:.GSZ.lgnfLS1.
3method-plain:guessme-plain 3method-plain:guessme-plain
4method-sha1:{SHA}KDR7D7GzCqeoXNa4AQNURXHGo58=
diff --git a/tests/test03.vtc b/tests/test03.vtc
new file mode 100644
index 0000000..052b4ed
--- a/dev/null
+++ b/tests/test03.vtc
@@ -0,0 +1,23 @@
1varnishtest "Test basic HTTP authentication: SHA1"
2
3server s1 {
4 rxreq
5 txresp
6} -start
7
8varnish v1 -vcl+backend {
9 import basicauth from "${vmod_topbuild}/src/.libs/libvmod_basicauth.so";
10 sub vcl_deliver {
11 set resp.http.result = basicauth.match("${vmod_topsrc}/tests/htpasswd", "Basic bWV0aG9kLXNoYTE6Z3Vlc3NtZS1zaGEx");
12 }
13} -start
14
15client c1 {
16 txreq -url "/"
17 rxresp
18 expect resp.http.result == true
19}
20
21client c1 -run
22
23

Return to:

Send suggestions and report system problems to the System administrator.