Import Debian changes 0.66.4-9
[hcoop/debian/courier-authlib.git] / cryptpassword.c
CommitLineData
d9898ee8 1/*
ac40fd9e 2** Copyright 2001-2008 Double Precision, Inc. See COPYING for
d9898ee8 3** distribution information.
4*/
5
6#if HAVE_CONFIG_H
7#include "courier_auth_config.h"
8#endif
9#include <string.h>
10#if HAVE_UNISTD_H
11#include <unistd.h>
12#endif
13#include <stdlib.h>
14#if HAVE_CRYPT_H
15#include <crypt.h>
16#endif
8d138742
CE
17#if HAVE_SYS_TIME_H
18#include <sys/time.h>
19#endif
d9898ee8 20#include "auth.h"
ac40fd9e 21#include "md5/md5.h"
22#include "sha1/sha1.h"
8d138742 23#include "random128/random128.h"
d9898ee8 24
d9898ee8 25
26#if HAVE_CRYPT
27#if NEED_CRYPT_PROTOTYPE
28extern char *crypt(const char *, const char *);
29#endif
30#endif
31
d9898ee8 32static const char crypt_salt[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
33
34static const char *crypt_hash(const char *pw)
35{
8d138742 36 random128binbuf randbuf;
d9898ee8 37 char salt[3];
38
8d138742 39 random128_binary(&randbuf);
d9898ee8 40
8d138742
CE
41 salt[0]=crypt_salt[ randbuf[0] % 64 ];
42 salt[1]=crypt_salt[ randbuf[1] % 64 ];
43 salt[2]=0;
d9898ee8 44
8d138742
CE
45 return (crypt(pw, salt));
46}
d9898ee8 47
8d138742
CE
48static const char *ssha_hash_int(const char *pw)
49{
50 random128binbuf randbuf;
d9898ee8 51
8d138742 52 random128_binary(&randbuf);
d9898ee8 53
8d138742 54 return ssha_hash(pw, randbuf);
d9898ee8 55}
56
d9898ee8 57static const char *crypt_md5_wrapper(const char *pw)
58{
59 struct timeval tv;
60 char salt[10];
61 int i;
62
63 gettimeofday(&tv, NULL);
64
65 tv.tv_sec |= tv.tv_usec;
66 tv.tv_sec ^= getpid();
67
68 strcpy(salt, "$1$");
69
70 for (i=3; i<8; i++)
71 {
72 salt[i]=crypt_salt[ tv.tv_sec % 64 ];
73 tv.tv_sec /= 64;
74 }
75
76 strcpy(salt+i, "$");
77
78 return (md5_crypt(pw, salt));
79}
d9898ee8 80
81char *authcryptpasswd(const char *password, const char *encryption_hint)
82{
83 const char *(*hash_func)(const char *)=0;
84 const char *pfix=0;
85 const char *p;
86 char *pp;
87
d9898ee8 88 if (!encryption_hint || strncmp(encryption_hint, "$1$", 3) == 0)
89 {
90 pfix="";
91 hash_func=crypt_md5_wrapper;
92 }
93
94 if (!encryption_hint || strncasecmp(encryption_hint, "{MD5}", 5) == 0)
95 {
96 hash_func= &md5_hash_courier;
97 pfix="{MD5}";
98 }
dd184caf 99
100 if (!encryption_hint || strncasecmp(encryption_hint, "{MD5RAW}", 5)
101 == 0)
102 {
103 hash_func= &md5_hash_raw;
104 pfix="{MD5RAW}";
105 }
d9898ee8 106
d9898ee8 107 if (!encryption_hint || strncasecmp(encryption_hint, "{SHA}", 5) == 0)
108 {
109 hash_func= &sha1_hash;
110 pfix="{SHA}";
111 }
112
8d138742
CE
113 if (!encryption_hint || strncasecmp(encryption_hint, "{SSHA}", 6) == 0)
114 {
115 hash_func= &ssha_hash_int;
116 pfix="{SSHA}";
117 }
118
d9898ee8 119 if (!encryption_hint ||
120 strncasecmp(encryption_hint, "{SHA256}", 8) == 0)
121 {
122 hash_func= &sha256_hash;
123 pfix="{SHA256}";
124 }
d9898ee8 125
8d138742
CE
126 if (!encryption_hint ||
127 strncasecmp(encryption_hint, "{SHA512}", 8) == 0)
128 {
129 hash_func= &sha512_hash;
130 pfix="{SHA512}";
131 }
132
d9898ee8 133 if (!hash_func)
134 {
135 hash_func= &crypt_hash;
136 pfix="{CRYPT}";
137 }
138
139 p= (*hash_func)(password);
140 if (!p || (pp=malloc(strlen(pfix)+strlen(p)+1)) == 0)
141 return (0);
142
143 return (strcat(strcpy(pp, pfix), p));
144}