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