sl@0
|
1 |
/* apps/passwd.c */
|
sl@0
|
2 |
|
sl@0
|
3 |
#if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
|
sl@0
|
4 |
# define NO_MD5CRYPT_1
|
sl@0
|
5 |
#endif
|
sl@0
|
6 |
|
sl@0
|
7 |
#if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
|
sl@0
|
8 |
|
sl@0
|
9 |
#include <assert.h>
|
sl@0
|
10 |
#include <string.h>
|
sl@0
|
11 |
|
sl@0
|
12 |
#include "apps.h"
|
sl@0
|
13 |
|
sl@0
|
14 |
#include <openssl/bio.h>
|
sl@0
|
15 |
#include <openssl/err.h>
|
sl@0
|
16 |
#include <openssl/evp.h>
|
sl@0
|
17 |
#include <openssl/rand.h>
|
sl@0
|
18 |
#ifndef OPENSSL_NO_DES
|
sl@0
|
19 |
# include <openssl/des.h>
|
sl@0
|
20 |
#endif
|
sl@0
|
21 |
#ifndef NO_MD5CRYPT_1
|
sl@0
|
22 |
# include <openssl/md5.h>
|
sl@0
|
23 |
#endif
|
sl@0
|
24 |
|
sl@0
|
25 |
|
sl@0
|
26 |
#undef PROG
|
sl@0
|
27 |
#define PROG passwd_main
|
sl@0
|
28 |
|
sl@0
|
29 |
|
sl@0
|
30 |
static unsigned const char cov_2char[64]={
|
sl@0
|
31 |
/* from crypto/des/fcrypt.c */
|
sl@0
|
32 |
0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
|
sl@0
|
33 |
0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
|
sl@0
|
34 |
0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
|
sl@0
|
35 |
0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
|
sl@0
|
36 |
0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
|
sl@0
|
37 |
0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
|
sl@0
|
38 |
0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
|
sl@0
|
39 |
0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
|
sl@0
|
40 |
};
|
sl@0
|
41 |
|
sl@0
|
42 |
static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
|
sl@0
|
43 |
char *passwd, BIO *out, int quiet, int table, int reverse,
|
sl@0
|
44 |
size_t pw_maxlen, int usecrypt, int use1, int useapr1);
|
sl@0
|
45 |
|
sl@0
|
46 |
/* -crypt - standard Unix password algorithm (default)
|
sl@0
|
47 |
* -1 - MD5-based password algorithm
|
sl@0
|
48 |
* -apr1 - MD5-based password algorithm, Apache variant
|
sl@0
|
49 |
* -salt string - salt
|
sl@0
|
50 |
* -in file - read passwords from file
|
sl@0
|
51 |
* -stdin - read passwords from stdin
|
sl@0
|
52 |
* -noverify - never verify when reading password from terminal
|
sl@0
|
53 |
* -quiet - no warnings
|
sl@0
|
54 |
* -table - format output as table
|
sl@0
|
55 |
* -reverse - switch table columns
|
sl@0
|
56 |
*/
|
sl@0
|
57 |
|
sl@0
|
58 |
|
sl@0
|
59 |
int MAIN(int, char **);
|
sl@0
|
60 |
|
sl@0
|
61 |
int MAIN(int argc, char **argv)
|
sl@0
|
62 |
{
|
sl@0
|
63 |
int ret = 1;
|
sl@0
|
64 |
char *infile = NULL;
|
sl@0
|
65 |
int in_stdin = 0;
|
sl@0
|
66 |
int in_noverify = 0;
|
sl@0
|
67 |
char *salt = NULL, *passwd = NULL, **passwds = NULL;
|
sl@0
|
68 |
char *salt_malloc = NULL, *passwd_malloc = NULL;
|
sl@0
|
69 |
size_t passwd_malloc_size = 0;
|
sl@0
|
70 |
int pw_source_defined = 0;
|
sl@0
|
71 |
BIO *in = NULL, *out = NULL;
|
sl@0
|
72 |
int i, badopt, opt_done;
|
sl@0
|
73 |
int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
|
sl@0
|
74 |
int usecrypt = 0, use1 = 0, useapr1 = 0;
|
sl@0
|
75 |
size_t pw_maxlen = 0;
|
sl@0
|
76 |
|
sl@0
|
77 |
apps_startup();
|
sl@0
|
78 |
|
sl@0
|
79 |
if (bio_err == NULL)
|
sl@0
|
80 |
if ((bio_err=BIO_new(BIO_s_file())) != NULL)
|
sl@0
|
81 |
BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
|
sl@0
|
82 |
|
sl@0
|
83 |
|
sl@0
|
84 |
if (!load_config(bio_err, NULL))
|
sl@0
|
85 |
goto err;
|
sl@0
|
86 |
out = BIO_new(BIO_s_file());
|
sl@0
|
87 |
if (out == NULL)
|
sl@0
|
88 |
goto err;
|
sl@0
|
89 |
BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
|
sl@0
|
90 |
#ifdef OPENSSL_SYS_VMS
|
sl@0
|
91 |
{
|
sl@0
|
92 |
BIO *tmpbio = BIO_new(BIO_f_linebuffer());
|
sl@0
|
93 |
out = BIO_push(tmpbio, out);
|
sl@0
|
94 |
}
|
sl@0
|
95 |
#endif
|
sl@0
|
96 |
|
sl@0
|
97 |
badopt = 0, opt_done = 0;
|
sl@0
|
98 |
i = 0;
|
sl@0
|
99 |
while (!badopt && !opt_done && argv[++i] != NULL)
|
sl@0
|
100 |
{
|
sl@0
|
101 |
if (strcmp(argv[i], "-crypt") == 0)
|
sl@0
|
102 |
usecrypt = 1;
|
sl@0
|
103 |
else if (strcmp(argv[i], "-1") == 0)
|
sl@0
|
104 |
use1 = 1;
|
sl@0
|
105 |
else if (strcmp(argv[i], "-apr1") == 0)
|
sl@0
|
106 |
useapr1 = 1;
|
sl@0
|
107 |
else if (strcmp(argv[i], "-salt") == 0)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
if ((argv[i+1] != NULL) && (salt == NULL))
|
sl@0
|
110 |
{
|
sl@0
|
111 |
passed_salt = 1;
|
sl@0
|
112 |
salt = argv[++i];
|
sl@0
|
113 |
}
|
sl@0
|
114 |
else
|
sl@0
|
115 |
badopt = 1;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
else if (strcmp(argv[i], "-in") == 0)
|
sl@0
|
118 |
{
|
sl@0
|
119 |
if ((argv[i+1] != NULL) && !pw_source_defined)
|
sl@0
|
120 |
{
|
sl@0
|
121 |
pw_source_defined = 1;
|
sl@0
|
122 |
infile = argv[++i];
|
sl@0
|
123 |
}
|
sl@0
|
124 |
else
|
sl@0
|
125 |
badopt = 1;
|
sl@0
|
126 |
}
|
sl@0
|
127 |
else if (strcmp(argv[i], "-stdin") == 0)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
if (!pw_source_defined)
|
sl@0
|
130 |
{
|
sl@0
|
131 |
pw_source_defined = 1;
|
sl@0
|
132 |
in_stdin = 1;
|
sl@0
|
133 |
}
|
sl@0
|
134 |
else
|
sl@0
|
135 |
badopt = 1;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
else if (strcmp(argv[i], "-noverify") == 0)
|
sl@0
|
138 |
in_noverify = 1;
|
sl@0
|
139 |
else if (strcmp(argv[i], "-quiet") == 0)
|
sl@0
|
140 |
quiet = 1;
|
sl@0
|
141 |
else if (strcmp(argv[i], "-table") == 0)
|
sl@0
|
142 |
table = 1;
|
sl@0
|
143 |
else if (strcmp(argv[i], "-reverse") == 0)
|
sl@0
|
144 |
reverse = 1;
|
sl@0
|
145 |
else if (argv[i][0] == '-')
|
sl@0
|
146 |
badopt = 1;
|
sl@0
|
147 |
else if (!pw_source_defined)
|
sl@0
|
148 |
/* non-option arguments, use as passwords */
|
sl@0
|
149 |
{
|
sl@0
|
150 |
pw_source_defined = 1;
|
sl@0
|
151 |
passwds = &argv[i];
|
sl@0
|
152 |
opt_done = 1;
|
sl@0
|
153 |
}
|
sl@0
|
154 |
else
|
sl@0
|
155 |
badopt = 1;
|
sl@0
|
156 |
}
|
sl@0
|
157 |
|
sl@0
|
158 |
if (!usecrypt && !use1 && !useapr1) /* use default */
|
sl@0
|
159 |
usecrypt = 1;
|
sl@0
|
160 |
if (usecrypt + use1 + useapr1 > 1) /* conflict */
|
sl@0
|
161 |
badopt = 1;
|
sl@0
|
162 |
|
sl@0
|
163 |
/* reject unsupported algorithms */
|
sl@0
|
164 |
#ifdef OPENSSL_NO_DES
|
sl@0
|
165 |
if (usecrypt) badopt = 1;
|
sl@0
|
166 |
#endif
|
sl@0
|
167 |
#ifdef NO_MD5CRYPT_1
|
sl@0
|
168 |
if (use1 || useapr1) badopt = 1;
|
sl@0
|
169 |
#endif
|
sl@0
|
170 |
|
sl@0
|
171 |
if (badopt)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
|
sl@0
|
174 |
BIO_printf(bio_err, "where options are\n");
|
sl@0
|
175 |
#ifndef OPENSSL_NO_DES
|
sl@0
|
176 |
BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n");
|
sl@0
|
177 |
#endif
|
sl@0
|
178 |
#ifndef NO_MD5CRYPT_1
|
sl@0
|
179 |
BIO_printf(bio_err, "-1 MD5-based password algorithm\n");
|
sl@0
|
180 |
BIO_printf(bio_err, "-apr1 MD5-based password algorithm, Apache variant\n");
|
sl@0
|
181 |
#endif
|
sl@0
|
182 |
BIO_printf(bio_err, "-salt string use provided salt\n");
|
sl@0
|
183 |
BIO_printf(bio_err, "-in file read passwords from file\n");
|
sl@0
|
184 |
BIO_printf(bio_err, "-stdin read passwords from stdin\n");
|
sl@0
|
185 |
BIO_printf(bio_err, "-noverify never verify when reading password from terminal\n");
|
sl@0
|
186 |
BIO_printf(bio_err, "-quiet no warnings\n");
|
sl@0
|
187 |
BIO_printf(bio_err, "-table format output as table\n");
|
sl@0
|
188 |
BIO_printf(bio_err, "-reverse switch table columns\n");
|
sl@0
|
189 |
|
sl@0
|
190 |
goto err;
|
sl@0
|
191 |
}
|
sl@0
|
192 |
|
sl@0
|
193 |
if ((infile != NULL) || in_stdin)
|
sl@0
|
194 |
{
|
sl@0
|
195 |
in = BIO_new(BIO_s_file());
|
sl@0
|
196 |
if (in == NULL)
|
sl@0
|
197 |
goto err;
|
sl@0
|
198 |
if (infile != NULL)
|
sl@0
|
199 |
{
|
sl@0
|
200 |
assert(in_stdin == 0);
|
sl@0
|
201 |
if (BIO_read_filename(in, infile) <= 0)
|
sl@0
|
202 |
goto err;
|
sl@0
|
203 |
}
|
sl@0
|
204 |
else
|
sl@0
|
205 |
{
|
sl@0
|
206 |
assert(in_stdin);
|
sl@0
|
207 |
BIO_set_fp(in, stdin, BIO_NOCLOSE);
|
sl@0
|
208 |
|
sl@0
|
209 |
}
|
sl@0
|
210 |
}
|
sl@0
|
211 |
|
sl@0
|
212 |
if (usecrypt)
|
sl@0
|
213 |
pw_maxlen = 8;
|
sl@0
|
214 |
else if (use1 || useapr1)
|
sl@0
|
215 |
pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */
|
sl@0
|
216 |
|
sl@0
|
217 |
if (passwds == NULL)
|
sl@0
|
218 |
{
|
sl@0
|
219 |
/* no passwords on the command line */
|
sl@0
|
220 |
|
sl@0
|
221 |
passwd_malloc_size = pw_maxlen + 2;
|
sl@0
|
222 |
/* longer than necessary so that we can warn about truncation */
|
sl@0
|
223 |
passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
|
sl@0
|
224 |
if (passwd_malloc == NULL)
|
sl@0
|
225 |
goto err;
|
sl@0
|
226 |
}
|
sl@0
|
227 |
|
sl@0
|
228 |
if ((in == NULL) && (passwds == NULL))
|
sl@0
|
229 |
{
|
sl@0
|
230 |
/* build a null-terminated list */
|
sl@0
|
231 |
static char *passwds_static[2] = {NULL, NULL};
|
sl@0
|
232 |
|
sl@0
|
233 |
passwds = passwds_static;
|
sl@0
|
234 |
if (in == NULL)
|
sl@0
|
235 |
if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", !(passed_salt || in_noverify)) != 0)
|
sl@0
|
236 |
goto err;
|
sl@0
|
237 |
passwds[0] = passwd_malloc;
|
sl@0
|
238 |
}
|
sl@0
|
239 |
|
sl@0
|
240 |
if (in == NULL)
|
sl@0
|
241 |
{
|
sl@0
|
242 |
assert(passwds != NULL);
|
sl@0
|
243 |
assert(*passwds != NULL);
|
sl@0
|
244 |
|
sl@0
|
245 |
do /* loop over list of passwords */
|
sl@0
|
246 |
{
|
sl@0
|
247 |
passwd = *passwds++;
|
sl@0
|
248 |
if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
|
sl@0
|
249 |
quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
|
sl@0
|
250 |
goto err;
|
sl@0
|
251 |
}
|
sl@0
|
252 |
while (*passwds != NULL);
|
sl@0
|
253 |
}
|
sl@0
|
254 |
else
|
sl@0
|
255 |
/* in != NULL */
|
sl@0
|
256 |
{
|
sl@0
|
257 |
int done;
|
sl@0
|
258 |
|
sl@0
|
259 |
assert (passwd != NULL);
|
sl@0
|
260 |
do
|
sl@0
|
261 |
{
|
sl@0
|
262 |
int r = BIO_gets(in, passwd, pw_maxlen + 1);
|
sl@0
|
263 |
if (r > 0)
|
sl@0
|
264 |
{
|
sl@0
|
265 |
char *c = (strchr(passwd, '\n')) ;
|
sl@0
|
266 |
if (c != NULL)
|
sl@0
|
267 |
*c = 0; /* truncate at newline */
|
sl@0
|
268 |
else
|
sl@0
|
269 |
{
|
sl@0
|
270 |
/* ignore rest of line */
|
sl@0
|
271 |
char trash[BUFSIZ];
|
sl@0
|
272 |
do
|
sl@0
|
273 |
r = BIO_gets(in, trash, sizeof trash);
|
sl@0
|
274 |
while ((r > 0) && (!strchr(trash, '\n')));
|
sl@0
|
275 |
}
|
sl@0
|
276 |
|
sl@0
|
277 |
if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
|
sl@0
|
278 |
quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
|
sl@0
|
279 |
goto err;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
done = (r <= 0);
|
sl@0
|
282 |
}
|
sl@0
|
283 |
while (!done);
|
sl@0
|
284 |
}
|
sl@0
|
285 |
ret = 0;
|
sl@0
|
286 |
|
sl@0
|
287 |
err:
|
sl@0
|
288 |
ERR_print_errors(bio_err);
|
sl@0
|
289 |
if (salt_malloc)
|
sl@0
|
290 |
OPENSSL_free(salt_malloc);
|
sl@0
|
291 |
if (passwd_malloc)
|
sl@0
|
292 |
OPENSSL_free(passwd_malloc);
|
sl@0
|
293 |
if (in)
|
sl@0
|
294 |
BIO_free(in);
|
sl@0
|
295 |
if (out)
|
sl@0
|
296 |
BIO_free_all(out);
|
sl@0
|
297 |
apps_shutdown();
|
sl@0
|
298 |
OPENSSL_EXIT(ret);
|
sl@0
|
299 |
}
|
sl@0
|
300 |
|
sl@0
|
301 |
|
sl@0
|
302 |
#ifndef NO_MD5CRYPT_1
|
sl@0
|
303 |
/* MD5-based password algorithm (should probably be available as a library
|
sl@0
|
304 |
* function; then the static buffer would not be acceptable).
|
sl@0
|
305 |
* For magic string "1", this should be compatible to the MD5-based BSD
|
sl@0
|
306 |
* password algorithm.
|
sl@0
|
307 |
* For 'magic' string "apr1", this is compatible to the MD5-based Apache
|
sl@0
|
308 |
* password algorithm.
|
sl@0
|
309 |
* (Apparently, the Apache password algorithm is identical except that the
|
sl@0
|
310 |
* 'magic' string was changed -- the laziest application of the NIH principle
|
sl@0
|
311 |
* I've ever encountered.)
|
sl@0
|
312 |
*/
|
sl@0
|
313 |
static char *md5crypt(const char *passwd, const char *magic, const char *salt)
|
sl@0
|
314 |
{
|
sl@0
|
315 |
static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
|
sl@0
|
316 |
unsigned char buf[MD5_DIGEST_LENGTH];
|
sl@0
|
317 |
char *salt_out;
|
sl@0
|
318 |
int n;
|
sl@0
|
319 |
unsigned int i;
|
sl@0
|
320 |
EVP_MD_CTX md,md2;
|
sl@0
|
321 |
size_t passwd_len, salt_len;
|
sl@0
|
322 |
|
sl@0
|
323 |
passwd_len = strlen(passwd);
|
sl@0
|
324 |
out_buf[0] = '$';
|
sl@0
|
325 |
out_buf[1] = 0;
|
sl@0
|
326 |
assert(strlen(magic) <= 4); /* "1" or "apr1" */
|
sl@0
|
327 |
strncat(out_buf, magic, 4);
|
sl@0
|
328 |
strncat(out_buf, "$", 1);
|
sl@0
|
329 |
strncat(out_buf, salt, 8);
|
sl@0
|
330 |
assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
|
sl@0
|
331 |
salt_out = out_buf + 2 + strlen(magic);
|
sl@0
|
332 |
salt_len = strlen(salt_out);
|
sl@0
|
333 |
assert(salt_len <= 8);
|
sl@0
|
334 |
|
sl@0
|
335 |
EVP_MD_CTX_init(&md);
|
sl@0
|
336 |
EVP_DigestInit_ex(&md,EVP_md5(), NULL);
|
sl@0
|
337 |
EVP_DigestUpdate(&md, passwd, passwd_len);
|
sl@0
|
338 |
EVP_DigestUpdate(&md, "$", 1);
|
sl@0
|
339 |
EVP_DigestUpdate(&md, magic, strlen(magic));
|
sl@0
|
340 |
EVP_DigestUpdate(&md, "$", 1);
|
sl@0
|
341 |
EVP_DigestUpdate(&md, salt_out, salt_len);
|
sl@0
|
342 |
|
sl@0
|
343 |
EVP_MD_CTX_init(&md2);
|
sl@0
|
344 |
EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
|
sl@0
|
345 |
EVP_DigestUpdate(&md2, passwd, passwd_len);
|
sl@0
|
346 |
EVP_DigestUpdate(&md2, salt_out, salt_len);
|
sl@0
|
347 |
EVP_DigestUpdate(&md2, passwd, passwd_len);
|
sl@0
|
348 |
EVP_DigestFinal_ex(&md2, buf, NULL);
|
sl@0
|
349 |
|
sl@0
|
350 |
for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
|
sl@0
|
351 |
EVP_DigestUpdate(&md, buf, sizeof buf);
|
sl@0
|
352 |
EVP_DigestUpdate(&md, buf, i);
|
sl@0
|
353 |
|
sl@0
|
354 |
n = passwd_len;
|
sl@0
|
355 |
while (n)
|
sl@0
|
356 |
{
|
sl@0
|
357 |
EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
|
sl@0
|
358 |
n >>= 1;
|
sl@0
|
359 |
}
|
sl@0
|
360 |
EVP_DigestFinal_ex(&md, buf, NULL);
|
sl@0
|
361 |
|
sl@0
|
362 |
for (i = 0; i < 1000; i++)
|
sl@0
|
363 |
{
|
sl@0
|
364 |
EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
|
sl@0
|
365 |
EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf,
|
sl@0
|
366 |
(i & 1) ? passwd_len : sizeof buf);
|
sl@0
|
367 |
if (i % 3)
|
sl@0
|
368 |
EVP_DigestUpdate(&md2, salt_out, salt_len);
|
sl@0
|
369 |
if (i % 7)
|
sl@0
|
370 |
EVP_DigestUpdate(&md2, passwd, passwd_len);
|
sl@0
|
371 |
EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd,
|
sl@0
|
372 |
(i & 1) ? sizeof buf : passwd_len);
|
sl@0
|
373 |
EVP_DigestFinal_ex(&md2, buf, NULL);
|
sl@0
|
374 |
}
|
sl@0
|
375 |
EVP_MD_CTX_cleanup(&md2);
|
sl@0
|
376 |
|
sl@0
|
377 |
{
|
sl@0
|
378 |
/* transform buf into output string */
|
sl@0
|
379 |
|
sl@0
|
380 |
unsigned char buf_perm[sizeof buf];
|
sl@0
|
381 |
int dest, source;
|
sl@0
|
382 |
char *output;
|
sl@0
|
383 |
|
sl@0
|
384 |
/* silly output permutation */
|
sl@0
|
385 |
for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
|
sl@0
|
386 |
buf_perm[dest] = buf[source];
|
sl@0
|
387 |
buf_perm[14] = buf[5];
|
sl@0
|
388 |
buf_perm[15] = buf[11];
|
sl@0
|
389 |
#ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
|
sl@0
|
390 |
assert(16 == sizeof buf_perm);
|
sl@0
|
391 |
#endif
|
sl@0
|
392 |
|
sl@0
|
393 |
output = salt_out + salt_len;
|
sl@0
|
394 |
assert(output == out_buf + strlen(out_buf));
|
sl@0
|
395 |
|
sl@0
|
396 |
*output++ = '$';
|
sl@0
|
397 |
|
sl@0
|
398 |
for (i = 0; i < 15; i += 3)
|
sl@0
|
399 |
{
|
sl@0
|
400 |
*output++ = cov_2char[buf_perm[i+2] & 0x3f];
|
sl@0
|
401 |
*output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
|
sl@0
|
402 |
(buf_perm[i+2] >> 6)];
|
sl@0
|
403 |
*output++ = cov_2char[((buf_perm[i] & 3) << 4) |
|
sl@0
|
404 |
(buf_perm[i+1] >> 4)];
|
sl@0
|
405 |
*output++ = cov_2char[buf_perm[i] >> 2];
|
sl@0
|
406 |
}
|
sl@0
|
407 |
assert(i == 15);
|
sl@0
|
408 |
*output++ = cov_2char[buf_perm[i] & 0x3f];
|
sl@0
|
409 |
*output++ = cov_2char[buf_perm[i] >> 6];
|
sl@0
|
410 |
*output = 0;
|
sl@0
|
411 |
assert(strlen(out_buf) < sizeof(out_buf));
|
sl@0
|
412 |
}
|
sl@0
|
413 |
EVP_MD_CTX_cleanup(&md);
|
sl@0
|
414 |
|
sl@0
|
415 |
return out_buf;
|
sl@0
|
416 |
}
|
sl@0
|
417 |
#endif
|
sl@0
|
418 |
|
sl@0
|
419 |
|
sl@0
|
420 |
static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
|
sl@0
|
421 |
char *passwd, BIO *out, int quiet, int table, int reverse,
|
sl@0
|
422 |
size_t pw_maxlen, int usecrypt, int use1, int useapr1)
|
sl@0
|
423 |
{
|
sl@0
|
424 |
char *hash = NULL;
|
sl@0
|
425 |
|
sl@0
|
426 |
assert(salt_p != NULL);
|
sl@0
|
427 |
assert(salt_malloc_p != NULL);
|
sl@0
|
428 |
|
sl@0
|
429 |
/* first make sure we have a salt */
|
sl@0
|
430 |
if (!passed_salt)
|
sl@0
|
431 |
{
|
sl@0
|
432 |
#ifndef OPENSSL_NO_DES
|
sl@0
|
433 |
if (usecrypt)
|
sl@0
|
434 |
{
|
sl@0
|
435 |
if (*salt_malloc_p == NULL)
|
sl@0
|
436 |
{
|
sl@0
|
437 |
*salt_p = *salt_malloc_p = OPENSSL_malloc(3);
|
sl@0
|
438 |
if (*salt_malloc_p == NULL)
|
sl@0
|
439 |
goto err;
|
sl@0
|
440 |
}
|
sl@0
|
441 |
if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
|
sl@0
|
442 |
goto err;
|
sl@0
|
443 |
(*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
|
sl@0
|
444 |
(*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
|
sl@0
|
445 |
(*salt_p)[2] = 0;
|
sl@0
|
446 |
#ifdef CHARSET_EBCDIC
|
sl@0
|
447 |
ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
|
sl@0
|
448 |
* back to ASCII */
|
sl@0
|
449 |
#endif
|
sl@0
|
450 |
}
|
sl@0
|
451 |
#endif /* !OPENSSL_NO_DES */
|
sl@0
|
452 |
|
sl@0
|
453 |
#ifndef NO_MD5CRYPT_1
|
sl@0
|
454 |
if (use1 || useapr1)
|
sl@0
|
455 |
{
|
sl@0
|
456 |
int i;
|
sl@0
|
457 |
|
sl@0
|
458 |
if (*salt_malloc_p == NULL)
|
sl@0
|
459 |
{
|
sl@0
|
460 |
*salt_p = *salt_malloc_p = OPENSSL_malloc(9);
|
sl@0
|
461 |
if (*salt_malloc_p == NULL)
|
sl@0
|
462 |
goto err;
|
sl@0
|
463 |
}
|
sl@0
|
464 |
if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
|
sl@0
|
465 |
goto err;
|
sl@0
|
466 |
|
sl@0
|
467 |
for (i = 0; i < 8; i++)
|
sl@0
|
468 |
(*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
|
sl@0
|
469 |
(*salt_p)[8] = 0;
|
sl@0
|
470 |
}
|
sl@0
|
471 |
#endif /* !NO_MD5CRYPT_1 */
|
sl@0
|
472 |
}
|
sl@0
|
473 |
|
sl@0
|
474 |
assert(*salt_p != NULL);
|
sl@0
|
475 |
|
sl@0
|
476 |
/* truncate password if necessary */
|
sl@0
|
477 |
if ((strlen(passwd) > pw_maxlen))
|
sl@0
|
478 |
{
|
sl@0
|
479 |
if (!quiet)
|
sl@0
|
480 |
/* XXX: really we should know how to print a size_t, not cast it */
|
sl@0
|
481 |
BIO_printf(bio_err, "Warning: truncating password to %u characters\n", (unsigned)pw_maxlen);
|
sl@0
|
482 |
passwd[pw_maxlen] = 0;
|
sl@0
|
483 |
}
|
sl@0
|
484 |
assert(strlen(passwd) <= pw_maxlen);
|
sl@0
|
485 |
|
sl@0
|
486 |
/* now compute password hash */
|
sl@0
|
487 |
#ifndef OPENSSL_NO_DES
|
sl@0
|
488 |
if (usecrypt)
|
sl@0
|
489 |
hash = DES_crypt(passwd, *salt_p);
|
sl@0
|
490 |
#endif
|
sl@0
|
491 |
#ifndef NO_MD5CRYPT_1
|
sl@0
|
492 |
if (use1 || useapr1)
|
sl@0
|
493 |
hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
|
sl@0
|
494 |
#endif
|
sl@0
|
495 |
assert(hash != NULL);
|
sl@0
|
496 |
|
sl@0
|
497 |
if (table && !reverse)
|
sl@0
|
498 |
BIO_printf(out, "%s\t%s\n", passwd, hash);
|
sl@0
|
499 |
else if (table && reverse)
|
sl@0
|
500 |
BIO_printf(out, "%s\t%s\n", hash, passwd);
|
sl@0
|
501 |
else
|
sl@0
|
502 |
BIO_printf(out, "%s\n", hash);
|
sl@0
|
503 |
return 1;
|
sl@0
|
504 |
|
sl@0
|
505 |
err:
|
sl@0
|
506 |
return 0;
|
sl@0
|
507 |
}
|
sl@0
|
508 |
#else
|
sl@0
|
509 |
|
sl@0
|
510 |
int MAIN(int argc, char **argv)
|
sl@0
|
511 |
{
|
sl@0
|
512 |
fputs("Program not available.\n", stderr)
|
sl@0
|
513 |
OPENSSL_EXIT(1);
|
sl@0
|
514 |
}
|
sl@0
|
515 |
#endif
|