Imported Debian patch 0.63.0-6
[hcoop/debian/courier-authlib.git] / checkpasswordsha1.c
1 /*
2 ** Copyright 2001-2008 Double Precision, Inc. See COPYING for
3 ** distribution information.
4 */
5
6 #if HAVE_CONFIG_H
7 #include "courier_auth_config.h"
8 #endif
9 #include "courierauthsasl.h"
10 #include <string.h>
11 #include <stdlib.h>
12 #if HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include "sha1/sha1.h"
16 #include "auth.h"
17
18 static const char rcsid[]="$Id: checkpasswordsha1.c,v 1.7 2008/12/25 14:52:38 mrsam Exp $";
19
20 int authcheckpasswordsha1(const char *password, const char *encrypted_password)
21 {
22 if (strncasecmp(encrypted_password, "{SHA}", 5) == 0)
23 {
24 return (strcmp(encrypted_password+5, sha1_hash(password)));
25 }
26 if (strncasecmp(encrypted_password, "{SHA256}", 8) == 0)
27 {
28 return (strcmp(encrypted_password+8, sha256_hash(password)));
29 }
30 if (strncasecmp(encrypted_password, "{SHA512}", 8) == 0)
31 {
32 return (strcmp(encrypted_password+8, sha512_hash(password)));
33 }
34 if (strncasecmp(encrypted_password, "{SSHA}", 6) == 0)
35 {
36 char *code = NULL;
37 int i;
38 SSHA_RAND rand;
39
40 code = strdup(encrypted_password+6);
41
42 if(code == NULL)
43 {
44 return (-1);
45 }
46
47 i = authsasl_frombase64(code);
48
49 if(i == -1 || i < sizeof(SSHA_RAND))
50 {
51 free(code);
52 return (-1);
53 }
54
55 memcpy((char *)rand, code+i-sizeof(SSHA_RAND),
56 sizeof(SSHA_RAND));
57
58 i=strcmp(encrypted_password+6, ssha_hash(password, rand));
59
60 free(code);
61 return i;
62
63 }
64 return (-1);
65 }