passwd.c revision 109998
1228753Smm/* apps/passwd.c */
2228753Smm
3228753Smm#if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
4228753Smm# define NO_MD5CRYPT_1
5228753Smm#endif
6228753Smm
7228753Smm#if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
8228753Smm
9228753Smm#include <assert.h>
10228753Smm#include <string.h>
11228753Smm
12228753Smm#include "apps.h"
13228753Smm
14228753Smm#include <openssl/bio.h>
15228753Smm#include <openssl/err.h>
16228753Smm#include <openssl/evp.h>
17228753Smm#include <openssl/rand.h>
18228753Smm#ifndef OPENSSL_NO_DES
19228753Smm# include <openssl/des.h>
20228753Smm#endif
21228753Smm#ifndef NO_MD5CRYPT_1
22228753Smm# include <openssl/md5.h>
23228753Smm#endif
24228753Smm
25228763Smm
26228753Smm#undef PROG
27228753Smm#define PROG passwd_main
28228753Smm
29228753Smm
30228753Smmstatic unsigned const char cov_2char[64]={
31232153Smm	/* from crypto/des/fcrypt.c */
32248616Smm	0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
33232153Smm	0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
34228753Smm	0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
35228753Smm	0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
36228753Smm	0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
37228753Smm	0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
38228753Smm	0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
39228753Smm	0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
40228753Smm};
41228753Smm
42228753Smmstatic int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
43228753Smm	char *passwd, BIO *out, int quiet, int table, int reverse,
44228753Smm	size_t pw_maxlen, int usecrypt, int use1, int useapr1);
45228753Smm
46228753Smm/* -crypt        - standard Unix password algorithm (default)
47228753Smm * -1            - MD5-based password algorithm
48228753Smm * -apr1         - MD5-based password algorithm, Apache variant
49228753Smm * -salt string  - salt
50238856Smm * -in file      - read passwords from file
51228753Smm * -stdin        - read passwords from stdin
52238856Smm * -noverify     - never verify when reading password from terminal
53228753Smm * -quiet        - no warnings
54228753Smm * -table        - format output as table
55232153Smm * -reverse      - switch table columns
56232153Smm */
57232153Smm
58232153Smmint MAIN(int, char **);
59232153Smm
60238856Smmint MAIN(int argc, char **argv)
61238856Smm	{
62238856Smm	int ret = 1;
63238856Smm	char *infile = NULL;
64238856Smm	int in_stdin = 0;
65238856Smm	int in_noverify = 0;
66238856Smm	char *salt = NULL, *passwd = NULL, **passwds = NULL;
67238856Smm	char *salt_malloc = NULL, *passwd_malloc = NULL;
68238856Smm	size_t passwd_malloc_size = 0;
69232153Smm	int pw_source_defined = 0;
70228753Smm	BIO *in = NULL, *out = NULL;
71228753Smm	int i, badopt, opt_done;
72228753Smm	int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
73228753Smm	int usecrypt = 0, use1 = 0, useapr1 = 0;
74228753Smm	size_t pw_maxlen = 0;
75228753Smm
76228753Smm	apps_startup();
77228753Smm
78228753Smm	if (bio_err == NULL)
79228753Smm		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
80228753Smm			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
81228753Smm
82228753Smm	if (!load_config(bio_err, NULL))
83228753Smm		goto err;
84228753Smm	out = BIO_new(BIO_s_file());
85228753Smm	if (out == NULL)
86232153Smm		goto err;
87228753Smm	BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
88228753Smm#ifdef OPENSSL_SYS_VMS
89228753Smm	{
90228753Smm	BIO *tmpbio = BIO_new(BIO_f_linebuffer());
91228753Smm	out = BIO_push(tmpbio, out);
92228753Smm	}
93228753Smm#endif
94228753Smm
95228753Smm	badopt = 0, opt_done = 0;
96228753Smm	i = 0;
97228753Smm	while (!badopt && !opt_done && argv[++i] != NULL)
98228753Smm		{
99228753Smm		if (strcmp(argv[i], "-crypt") == 0)
100228753Smm			usecrypt = 1;
101228753Smm		else if (strcmp(argv[i], "-1") == 0)
102228753Smm			use1 = 1;
103228753Smm		else if (strcmp(argv[i], "-apr1") == 0)
104228753Smm			useapr1 = 1;
105228753Smm		else if (strcmp(argv[i], "-salt") == 0)
106228753Smm			{
107228753Smm			if ((argv[i+1] != NULL) && (salt == NULL))
108228753Smm				{
109228753Smm				passed_salt = 1;
110228753Smm				salt = argv[++i];
111228753Smm				}
112228753Smm			else
113228753Smm				badopt = 1;
114228753Smm			}
115228753Smm		else if (strcmp(argv[i], "-in") == 0)
116232153Smm			{
117228753Smm			if ((argv[i+1] != NULL) && !pw_source_defined)
118228753Smm				{
119228753Smm				pw_source_defined = 1;
120228753Smm				infile = argv[++i];
121228753Smm				}
122228753Smm			else
123228753Smm				badopt = 1;
124228753Smm			}
125228753Smm		else if (strcmp(argv[i], "-stdin") == 0)
126228753Smm			{
127228753Smm			if (!pw_source_defined)
128228753Smm				{
129228753Smm				pw_source_defined = 1;
130228753Smm				in_stdin = 1;
131228753Smm				}
132228753Smm			else
133228753Smm				badopt = 1;
134228753Smm			}
135228753Smm		else if (strcmp(argv[i], "-noverify") == 0)
136228753Smm			in_noverify = 1;
137228753Smm		else if (strcmp(argv[i], "-quiet") == 0)
138228753Smm			quiet = 1;
139228753Smm		else if (strcmp(argv[i], "-table") == 0)
140238856Smm			table = 1;
141238856Smm		else if (strcmp(argv[i], "-reverse") == 0)
142238856Smm			reverse = 1;
143238856Smm		else if (argv[i][0] == '-')
144238856Smm			badopt = 1;
145238856Smm		else if (!pw_source_defined)
146238856Smm			/* non-option arguments, use as passwords */
147238856Smm			{
148238856Smm			pw_source_defined = 1;
149238856Smm			passwds = &argv[i];
150238856Smm			opt_done = 1;
151238856Smm			}
152228753Smm		else
153228753Smm			badopt = 1;
154228753Smm		}
155228753Smm
156228753Smm	if (!usecrypt && !use1 && !useapr1) /* use default */
157228753Smm		usecrypt = 1;
158228753Smm	if (usecrypt + use1 + useapr1 > 1) /* conflict */
159228753Smm		badopt = 1;
160228753Smm
161228753Smm	/* reject unsupported algorithms */
162228753Smm#ifdef OPENSSL_NO_DES
163228753Smm	if (usecrypt) badopt = 1;
164232153Smm#endif
165232153Smm#ifdef NO_MD5CRYPT_1
166232153Smm	if (use1 || useapr1) badopt = 1;
167232153Smm#endif
168232153Smm
169232153Smm	if (badopt)
170232153Smm		{
171232153Smm		BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
172232153Smm		BIO_printf(bio_err, "where options are\n");
173228753Smm#ifndef OPENSSL_NO_DES
174228753Smm		BIO_printf(bio_err, "-crypt             standard Unix password algorithm (default)\n");
175228753Smm#endif
176228753Smm#ifndef NO_MD5CRYPT_1
177228753Smm		BIO_printf(bio_err, "-1                 MD5-based password algorithm\n");
178228753Smm		BIO_printf(bio_err, "-apr1              MD5-based password algorithm, Apache variant\n");
179228753Smm#endif
180228753Smm		BIO_printf(bio_err, "-salt string       use provided salt\n");
181228753Smm		BIO_printf(bio_err, "-in file           read passwords from file\n");
182228753Smm		BIO_printf(bio_err, "-stdin             read passwords from stdin\n");
183228753Smm		BIO_printf(bio_err, "-noverify          never verify when reading password from terminal\n");
184228753Smm		BIO_printf(bio_err, "-quiet             no warnings\n");
185228753Smm		BIO_printf(bio_err, "-table             format output as table\n");
186228753Smm		BIO_printf(bio_err, "-reverse           switch table columns\n");
187228753Smm
188228753Smm		goto err;
189228753Smm		}
190228753Smm
191228753Smm	if ((infile != NULL) || in_stdin)
192228753Smm		{
193228753Smm		in = BIO_new(BIO_s_file());
194228753Smm		if (in == NULL)
195228753Smm			goto err;
196228753Smm		if (infile != NULL)
197228753Smm			{
198228753Smm			assert(in_stdin == 0);
199228753Smm			if (BIO_read_filename(in, infile) <= 0)
200228753Smm				goto err;
201232153Smm			}
202228753Smm		else
203228753Smm			{
204228753Smm			assert(in_stdin);
205228753Smm			BIO_set_fp(in, stdin, BIO_NOCLOSE);
206228753Smm			}
207228753Smm		}
208228753Smm
209232153Smm	if (usecrypt)
210228753Smm		pw_maxlen = 8;
211228753Smm	else if (use1 || useapr1)
212228753Smm		pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */
213228753Smm
214232153Smm	if (passwds == NULL)
215228753Smm		{
216232153Smm		/* no passwords on the command line */
217228753Smm
218228753Smm		passwd_malloc_size = pw_maxlen + 2;
219228753Smm		/* longer than necessary so that we can warn about truncation */
220228753Smm		passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
221228753Smm		if (passwd_malloc == NULL)
222228753Smm			goto err;
223228753Smm		}
224232153Smm
225228753Smm	if ((in == NULL) && (passwds == NULL))
226228753Smm		{
227228753Smm		/* build a null-terminated list */
228228753Smm		static char *passwds_static[2] = {NULL, NULL};
229232153Smm
230228753Smm		passwds = passwds_static;
231228753Smm		if (in == NULL)
232228753Smm			if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", !(passed_salt || in_noverify)) != 0)
233228753Smm				goto err;
234228753Smm		passwds[0] = passwd_malloc;
235232153Smm		}
236228753Smm
237228753Smm	if (in == NULL)
238228753Smm		{
239228753Smm		assert(passwds != NULL);
240228753Smm		assert(*passwds != NULL);
241228753Smm
242232153Smm		do /* loop over list of passwords */
243232153Smm			{
244232153Smm			passwd = *passwds++;
245232153Smm			if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
246228753Smm				quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
247228753Smm				goto err;
248228753Smm			}
249228753Smm		while (*passwds != NULL);
250228753Smm		}
251232153Smm	else
252228753Smm		/* in != NULL */
253228753Smm		{
254228753Smm		int done;
255228753Smm
256228753Smm		assert (passwd != NULL);
257228753Smm		do
258228753Smm			{
259228753Smm			int r = BIO_gets(in, passwd, pw_maxlen + 1);
260228753Smm			if (r > 0)
261228753Smm				{
262228753Smm				char *c = (strchr(passwd, '\n')) ;
263228753Smm				if (c != NULL)
264228753Smm					*c = 0; /* truncate at newline */
265228753Smm				else
266228753Smm					{
267228753Smm					/* ignore rest of line */
268228753Smm					char trash[BUFSIZ];
269232153Smm					do
270228753Smm						r = BIO_gets(in, trash, sizeof trash);
271228753Smm					while ((r > 0) && (!strchr(trash, '\n')));
272228753Smm					}
273228753Smm
274228753Smm				if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
275228753Smm					quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
276228753Smm					goto err;
277228753Smm				}
278228753Smm			done = (r <= 0);
279228753Smm			}
280228753Smm		while (!done);
281228753Smm		}
282228753Smm	ret = 0;
283228753Smm
284228753Smmerr:
285228753Smm	ERR_print_errors(bio_err);
286228753Smm	if (salt_malloc)
287228753Smm		OPENSSL_free(salt_malloc);
288228753Smm	if (passwd_malloc)
289228753Smm		OPENSSL_free(passwd_malloc);
290228753Smm	if (in)
291228753Smm		BIO_free(in);
292228753Smm	if (out)
293228753Smm		BIO_free_all(out);
294228753Smm	apps_shutdown();
295228753Smm	OPENSSL_EXIT(ret);
296228753Smm	}
297228753Smm
298228753Smm
299232153Smm#ifndef NO_MD5CRYPT_1
300228753Smm/* MD5-based password algorithm (should probably be available as a library
301228753Smm * function; then the static buffer would not be acceptable).
302228753Smm * For magic string "1", this should be compatible to the MD5-based BSD
303228753Smm * password algorithm.
304232153Smm * For 'magic' string "apr1", this is compatible to the MD5-based Apache
305228753Smm * password algorithm.
306228753Smm * (Apparently, the Apache password algorithm is identical except that the
307228753Smm * 'magic' string was changed -- the laziest application of the NIH principle
308228753Smm * I've ever encountered.)
309228753Smm */
310228753Smmstatic char *md5crypt(const char *passwd, const char *magic, const char *salt)
311228753Smm	{
312228753Smm	static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
313228753Smm	unsigned char buf[MD5_DIGEST_LENGTH];
314228753Smm	char *salt_out;
315228753Smm	int n, i;
316238856Smm	EVP_MD_CTX md,md2;
317238856Smm	size_t passwd_len, salt_len;
318238856Smm
319238856Smm	passwd_len = strlen(passwd);
320228753Smm	out_buf[0] = '$';
321228753Smm	out_buf[1] = 0;
322228753Smm	assert(strlen(magic) <= 4); /* "1" or "apr1" */
323228753Smm	strncat(out_buf, magic, 4);
324232153Smm	strncat(out_buf, "$", 1);
325232153Smm	strncat(out_buf, salt, 8);
326232153Smm	assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
327232153Smm	salt_out = out_buf + 2 + strlen(magic);
328232153Smm	salt_len = strlen(salt_out);
329232153Smm	assert(salt_len <= 8);
330228753Smm
331232153Smm	EVP_MD_CTX_init(&md);
332232153Smm	EVP_DigestInit_ex(&md,EVP_md5(), NULL);
333232153Smm	EVP_DigestUpdate(&md, passwd, passwd_len);
334228753Smm	EVP_DigestUpdate(&md, "$", 1);
335228753Smm	EVP_DigestUpdate(&md, magic, strlen(magic));
336228753Smm	EVP_DigestUpdate(&md, "$", 1);
337228753Smm	EVP_DigestUpdate(&md, salt_out, salt_len);
338228753Smm
339228753Smm	EVP_MD_CTX_init(&md2);
340228753Smm	EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
341228753Smm	EVP_DigestUpdate(&md2, passwd, passwd_len);
342228753Smm	EVP_DigestUpdate(&md2, salt_out, salt_len);
343228753Smm	EVP_DigestUpdate(&md2, passwd, passwd_len);
344228753Smm	EVP_DigestFinal_ex(&md2, buf, NULL);
345228753Smm
346228753Smm	for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
347232153Smm		EVP_DigestUpdate(&md, buf, sizeof buf);
348228753Smm	EVP_DigestUpdate(&md, buf, i);
349232153Smm
350232153Smm	n = passwd_len;
351232153Smm	while (n)
352232153Smm		{
353232153Smm		EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
354232153Smm		n >>= 1;
355232153Smm		}
356232153Smm	EVP_DigestFinal_ex(&md, buf, NULL);
357232153Smm
358232153Smm	for (i = 0; i < 1000; i++)
359232153Smm		{
360232153Smm		EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
361232153Smm		EVP_DigestUpdate(&md2, (i & 1) ? (unsigned char *) passwd : buf,
362232153Smm		                       (i & 1) ? passwd_len : sizeof buf);
363232153Smm		if (i % 3)
364232153Smm			EVP_DigestUpdate(&md2, salt_out, salt_len);
365232153Smm		if (i % 7)
366232153Smm			EVP_DigestUpdate(&md2, passwd, passwd_len);
367232153Smm		EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned char *) passwd,
368228753Smm		                       (i & 1) ? sizeof buf : passwd_len);
369232153Smm		EVP_DigestFinal_ex(&md2, buf, NULL);
370232153Smm		}
371232153Smm	EVP_MD_CTX_cleanup(&md2);
372232153Smm
373228753Smm	 {
374232153Smm		/* transform buf into output string */
375232153Smm
376232153Smm		unsigned char buf_perm[sizeof buf];
377232153Smm		int dest, source;
378232153Smm		char *output;
379232153Smm
380232153Smm		/* silly output permutation */
381232153Smm		for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
382232153Smm			buf_perm[dest] = buf[source];
383232153Smm		buf_perm[14] = buf[5];
384232153Smm		buf_perm[15] = buf[11];
385232153Smm#ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
386232153Smm		assert(16 == sizeof buf_perm);
387232153Smm#endif
388232153Smm
389232153Smm		output = salt_out + salt_len;
390232153Smm		assert(output == out_buf + strlen(out_buf));
391232153Smm
392232153Smm		*output++ = '$';
393232153Smm
394232153Smm		for (i = 0; i < 15; i += 3)
395232153Smm			{
396232153Smm			*output++ = cov_2char[buf_perm[i+2] & 0x3f];
397232153Smm			*output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
398232153Smm				                  (buf_perm[i+2] >> 6)];
399232153Smm			*output++ = cov_2char[((buf_perm[i] & 3) << 4) |
400232153Smm				                  (buf_perm[i+1] >> 4)];
401232153Smm			*output++ = cov_2char[buf_perm[i] >> 2];
402232153Smm			}
403232153Smm		assert(i == 15);
404232153Smm		*output++ = cov_2char[buf_perm[i] & 0x3f];
405232153Smm		*output++ = cov_2char[buf_perm[i] >> 6];
406232153Smm		*output = 0;
407232153Smm		assert(strlen(out_buf) < sizeof(out_buf));
408232153Smm	 }
409232153Smm	EVP_MD_CTX_cleanup(&md);
410232153Smm
411232153Smm	return out_buf;
412232153Smm	}
413232153Smm#endif
414232153Smm
415232153Smm
416232153Smmstatic int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
417232153Smm	char *passwd, BIO *out,	int quiet, int table, int reverse,
418232153Smm	size_t pw_maxlen, int usecrypt, int use1, int useapr1)
419232153Smm	{
420232153Smm	char *hash = NULL;
421232153Smm
422232153Smm	assert(salt_p != NULL);
423232153Smm	assert(salt_malloc_p != NULL);
424232153Smm
425228753Smm	/* first make sure we have a salt */
426228753Smm	if (!passed_salt)
427228753Smm		{
428228753Smm#ifndef OPENSSL_NO_DES
429228753Smm		if (usecrypt)
430232153Smm			{
431232153Smm			if (*salt_malloc_p == NULL)
432232153Smm				{
433228753Smm				*salt_p = *salt_malloc_p = OPENSSL_malloc(3);
434228753Smm				if (*salt_malloc_p == NULL)
435228753Smm					goto err;
436228753Smm				}
437228753Smm			if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
438228753Smm				goto err;
439228753Smm			(*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
440228753Smm			(*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
441228753Smm			(*salt_p)[2] = 0;
442228753Smm#ifdef CHARSET_EBCDIC
443228753Smm			ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
444232153Smm			                                    * back to ASCII */
445228753Smm#endif
446228753Smm			}
447232153Smm#endif /* !OPENSSL_NO_DES */
448228753Smm
449228753Smm#ifndef NO_MD5CRYPT_1
450228753Smm		if (use1 || useapr1)
451228753Smm			{
452228753Smm			int i;
453228753Smm
454232153Smm			if (*salt_malloc_p == NULL)
455228753Smm				{
456228753Smm				*salt_p = *salt_malloc_p = OPENSSL_malloc(9);
457228753Smm				if (*salt_malloc_p == NULL)
458228753Smm					goto err;
459228753Smm				}
460228753Smm			if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
461228753Smm				goto err;
462228753Smm
463228753Smm			for (i = 0; i < 8; i++)
464228753Smm				(*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
465228753Smm			(*salt_p)[8] = 0;
466228753Smm			}
467228753Smm#endif /* !NO_MD5CRYPT_1 */
468232153Smm		}
469232153Smm
470232153Smm	assert(*salt_p != NULL);
471228753Smm
472232153Smm	/* truncate password if necessary */
473232153Smm	if ((strlen(passwd) > pw_maxlen))
474228753Smm		{
475232153Smm		if (!quiet)
476228753Smm			BIO_printf(bio_err, "Warning: truncating password to %u characters\n", pw_maxlen);
477228753Smm		passwd[pw_maxlen] = 0;
478228753Smm		}
479228753Smm	assert(strlen(passwd) <= pw_maxlen);
480228753Smm
481232153Smm	/* now compute password hash */
482232153Smm#ifndef OPENSSL_NO_DES
483228753Smm	if (usecrypt)
484228753Smm		hash = DES_crypt(passwd, *salt_p);
485228753Smm#endif
486228753Smm#ifndef NO_MD5CRYPT_1
487232153Smm	if (use1 || useapr1)
488232153Smm		hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
489232153Smm#endif
490232153Smm	assert(hash != NULL);
491228753Smm
492228753Smm	if (table && !reverse)
493228753Smm		BIO_printf(out, "%s\t%s\n", passwd, hash);
494228753Smm	else if (table && reverse)
495228753Smm		BIO_printf(out, "%s\t%s\n", hash, passwd);
496228753Smm	else
497228753Smm		BIO_printf(out, "%s\n", hash);
498228753Smm	return 1;
499228753Smm
500228753Smmerr:
501228753Smm	return 0;
502228753Smm	}
503228753Smm#else
504228753Smm
505228753Smmint MAIN(int argc, char **argv)
506228753Smm	{
507228753Smm	fputs("Program not available.\n", stderr)
508228753Smm	OPENSSL_EXIT(1);
509228753Smm	}
510228753Smm#endif
511228753Smm