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