Debian Codegraph @

modules/crypto/posix.c

raw
1 /*
2 * Copyright (c) 2005 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * POSIX-style crypt(3) wrapper.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "crypto/posix", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 #if defined(HAVE_CRYPT)
19
20 static const char *posixc_crypt_string(const char *key, const char *salt)
21 {
22 const char *result;
23 char salt2[3];
24 static int warned;
25
26 result = crypt(key, salt);
27 if (!strncmp(salt, "$1$", 3) && strncmp(result, "$1$", 3))
28 {
29 if (!warned)
30 slog(LG_INFO, "posixc_crypt_string(): broken crypt() detected, falling back to DES");
31 warned = 1;
32 salt2[0] = salt[3];
33 salt2[1] = salt[4];
34 salt2[2] = '\0';
35 result = crypt(key, salt2);
36 }
37 return result;
38 }
39
40 static const char *(*crypt_impl_)(const char *key, const char *salt) = &posixc_crypt_string;
41
42 #elif defined(HAVE_OPENSSL)
43
44 #include <openssl/bio.h>
45 #include <openssl/err.h>
46 #include <openssl/evp.h>
47 #include <openssl/md5.h>
48
49 /* Yanked out of OpenSSL-1.0.0g apps/passwd.c */
50 static unsigned const char cov_2char[64] = {
51 /* from crypto/des/fcrypt.c */
52 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
53 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
54 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
55 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
56 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
57 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
58 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
59 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
60 };
61
62 /* MD5-based password algorithm (should probably be available as a library
63 * function; then the static buffer would not be acceptable).
64 * For magic string "1", this should be compatible to the MD5-based BSD
65 * password algorithm.
66 * For 'magic' string "apr1", this is compatible to the MD5-based Apache
67 * password algorithm.
68 * (Apparently, the Apache password algorithm is identical except that the
69 * 'magic' string was changed -- the laziest application of the NIH principle
70 * I've ever encountered.)
71 */
72 static const char *openssl_md5crypt(const char *passwd, const char *salt)
73 {
74 const char *magic = "1";
75 static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
76 unsigned char buf[MD5_DIGEST_LENGTH];
77 char *salt_out;
78 int n;
79 unsigned int i;
80 EVP_MD_CTX md,md2;
81 size_t passwd_len, salt_len;
82
83 passwd_len = strlen(passwd);
84 out_buf[0] = '$';
85 out_buf[1] = 0;
86 soft_assert(strlen(magic) <= 4); /* "1" or "apr1" */
87 strncat(out_buf, magic, 4);
88 strncat(out_buf, "$", 1);
89 strncat(out_buf, salt, 8);
90 soft_assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
91 salt_out = out_buf + 2 + strlen(magic);
92 salt_len = strlen(salt_out);
93 soft_assert(salt_len <= 8);
94
95 EVP_MD_CTX_init(&md);
96 EVP_DigestInit_ex(&md,EVP_md5(), NULL);
97 EVP_DigestUpdate(&md, passwd, passwd_len);
98 EVP_DigestUpdate(&md, "$", 1);
99 EVP_DigestUpdate(&md, magic, strlen(magic));
100 EVP_DigestUpdate(&md, "$", 1);
101 EVP_DigestUpdate(&md, salt_out, salt_len);
102
103 EVP_MD_CTX_init(&md2);
104 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
105 EVP_DigestUpdate(&md2, passwd, passwd_len);
106 EVP_DigestUpdate(&md2, salt_out, salt_len);
107 EVP_DigestUpdate(&md2, passwd, passwd_len);
108 EVP_DigestFinal_ex(&md2, buf, NULL);
109
110 for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
111 EVP_DigestUpdate(&md, buf, sizeof buf);
112 EVP_DigestUpdate(&md, buf, i);
113
114 n = passwd_len;
115 while (n)
116 {
117 EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
118 n >>= 1;
119 }
120 EVP_DigestFinal_ex(&md, buf, NULL);
121
122 for (i = 0; i < 1000; i++)
123 {
124 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
125 EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf,
126 (i & 1) ? passwd_len : sizeof buf);
127 if (i % 3)
128 EVP_DigestUpdate(&md2, salt_out, salt_len);
129 if (i % 7)
130 EVP_DigestUpdate(&md2, passwd, passwd_len);
131 EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd,
132 (i & 1) ? sizeof buf : passwd_len);
133 EVP_DigestFinal_ex(&md2, buf, NULL);
134 }
135
136 EVP_MD_CTX_cleanup(&md2);
137
138 {
139 /* transform buf into output string */
140 unsigned char buf_perm[sizeof buf];
141 int dest, source;
142 char *output;
143
144 /* silly output permutation */
145 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
146 buf_perm[dest] = buf[source];
147 buf_perm[14] = buf[5];
148 buf_perm[15] = buf[11];
149 #ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
150 soft_assert(16 == sizeof buf_perm);
151 #endif
152
153 output = salt_out + salt_len;
154 soft_assert(output == out_buf + strlen(out_buf));
155
156 *output++ = '$';
157
158 for (i = 0; i < 15; i += 3)
159 {
160 *output++ = cov_2char[buf_perm[i+2] & 0x3f];
161 *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
162 (buf_perm[i+2] >> 6)];
163 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
164 (buf_perm[i+1] >> 4)];
165 *output++ = cov_2char[buf_perm[i] >> 2];
166 }
167 soft_assert(i == 15);
168 *output++ = cov_2char[buf_perm[i] & 0x3f];
169 *output++ = cov_2char[buf_perm[i] >> 6];
170 *output = 0;
171 soft_assert(strlen(out_buf) < sizeof(out_buf));
172 }
173 EVP_MD_CTX_cleanup(&md);
174
175 return out_buf;
176 }
177
178 static const char *openssl_crypt_string(const char *key, const char *salt)
179 {
180 char real_salt[BUFSIZE];
181 char *term;
182
183 mowgli_strlcpy(real_salt, salt + 3, sizeof real_salt);
184
185 term = strrchr(real_salt, '$');
186
187 if (term == NULL)
188 return NULL;
189
190 *term = '\0';
191
192 return openssl_md5crypt(key, real_salt);
193 }
194
195 static const char *(*crypt_impl_)(const char *key, const char *salt) = &openssl_crypt_string;
196
197 #else
198
199 #warning could not find a crypt impl, sorry (this is stubbed)
200
201 static const char *stub_crypt_string(const char *key, const char *salt)
202 {
203 return key;
204 }
205
206 static const char *(*crypt_impl_)(const char *key, const char *salt) = &stub_crypt_string;
207
208 #endif
209
210 static crypt_impl_t posix_crypt_impl = {
211 .id = "posix",
212 };
213
214 void _modinit(module_t *m)
215 {
216 posix_crypt_impl.crypt = crypt_impl_;
217
218 #if defined(HAVE_CRYPT) || defined(HAVE_OPENSSL)
219 crypt_register(&posix_crypt_impl);
220 #endif
221 }
222
223 void _moddeinit(module_unload_intent_t intent)
224 {
225 #if defined(HAVE_CRYPT) || defined(HAVE_OPENSSL)
226 crypt_unregister(&posix_crypt_impl);
227 #endif
228 }
229
230 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
231 * vim:ts=8
232 * vim:sw=8
233 * vim:noexpandtab
234 */