Import Upstream version 0.66.4
[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
19 int authcheckpasswordsha1(const char *password, const char *encrypted_password)
20 {
21 if (strncasecmp(encrypted_password, "{SHA}", 5) == 0)
22 {
23 return (strcmp(encrypted_password+5, sha1_hash(password)));
24 }
25 if (strncasecmp(encrypted_password, "{SHA256}", 8) == 0)
26 {
27 return (strcmp(encrypted_password+8, sha256_hash(password)));
28 }
29 if (strncasecmp(encrypted_password, "{SHA512}", 8) == 0)
30 {
31 return (strcmp(encrypted_password+8, sha512_hash(password)));
32 }
33 if (strncasecmp(encrypted_password, "{SSHA}", 6) == 0)
34 {
35 char *code = NULL;
36 int i;
37 SSHA_RAND rand;
38
39 code = strdup(encrypted_password+6);
40
41 if(code == NULL)
42 {
43 return (-1);
44 }
45
46 i = authsasl_frombase64(code);
47
48 if(i == -1 || i < sizeof(SSHA_RAND))
49 {
50 free(code);
51 return (-1);
52 }
53
54 memcpy((char *)rand, code+i-sizeof(SSHA_RAND),
55 sizeof(SSHA_RAND));
56
57 i=strcmp(encrypted_password+6, ssha_hash(password, rand));
58
59 free(code);
60 return i;
61
62 }
63 return (-1);
64 }