Annotation of src/usr.bin/openssl/passwd.c, Revision 1.4
1.4 ! jsing 1: /* $OpenBSD: passwd.c,v 1.3 2015/01/05 15:25:39 jsing Exp $ */
1.1 jsing 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:
1.3 jsing 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: } passwd_config;
55:
56: static 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 = &passwd_config.use1,
63: },
64: {
65: .name = "apr1",
66: .desc = "Use apr1 algorithm (Apache variant of BSD algorithm)",
67: .type = OPTION_FLAG,
68: .opt.flag = &passwd_config.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 = &passwd_config.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 = &passwd_config.infile,
85: },
86: {
87: .name = "noverify",
88: .desc = "Do not verify password",
89: .type = OPTION_FLAG,
90: .opt.flag = &passwd_config.noverify,
91: },
92: {
93: .name = "quiet",
94: .desc = "Do not output warnings",
95: .type = OPTION_FLAG,
96: .opt.flag = &passwd_config.quiet,
97: },
98: {
99: .name = "reverse",
100: .desc = "Reverse table columns (requires -table)",
101: .type = OPTION_FLAG,
102: .opt.flag = &passwd_config.reverse,
103: },
104: {
105: .name = "salt",
106: .argname = "string",
107: .desc = "Use specified salt",
108: .type = OPTION_ARG,
109: .opt.arg = &passwd_config.salt,
110: },
111: {
112: .name = "stdin",
113: .desc = "Read passwords from stdin",
114: .type = OPTION_FLAG,
115: .opt.flag = &passwd_config.in_stdin,
116: },
117: {
118: .name = "table",
119: .desc = "Output cleartext and hashed passwords (tab separated)",
120: .type = OPTION_FLAG,
121: .opt.flag = &passwd_config.table,
122: },
123: { NULL },
124: };
125:
126: static 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: }
1.1 jsing 134:
135: int
136: passwd_main(int argc, char **argv)
137: {
1.3 jsing 138: char *passwd = NULL, **passwds = NULL;
1.1 jsing 139: char *salt_malloc = NULL, *passwd_malloc = NULL;
140: size_t passwd_malloc_size = 0;
141: BIO *in = NULL, *out = NULL;
1.3 jsing 142: int badopt = 0;
143: int passed_salt = 0;
1.1 jsing 144: size_t pw_maxlen = 0;
1.3 jsing 145: int argsused;
146: int ret = 1;
1.1 jsing 147:
1.3 jsing 148: memset(&passwd_config, 0, sizeof(passwd_config));
149:
150: if (options_parse(argc, argv, passwd_options, NULL, &argsused) != 0) {
151: passwd_usage();
1.1 jsing 152: goto err;
1.3 jsing 153: }
1.1 jsing 154:
1.3 jsing 155: if (argsused < argc)
156: passwds = &argv[argsused];
157: if (passwd_config.salt != NULL)
158: passed_salt = 1;
159:
160: if (!passwd_config.usecrypt && !passwd_config.use1 &&
161: !passwd_config.useapr1)
162: passwd_config.usecrypt = 1; /* use default */
163: if (passwd_config.usecrypt + passwd_config.use1 +
164: passwd_config.useapr1 > 1)
165: badopt = 1; /* conflicting options */
1.1 jsing 166:
1.3 jsing 167: /* Reject unsupported algorithms */
1.1 jsing 168: #ifdef OPENSSL_NO_DES
1.3 jsing 169: if (passwd_config.usecrypt)
1.1 jsing 170: badopt = 1;
171: #endif
172: #ifdef NO_MD5CRYPT_1
1.3 jsing 173: if (passwd_config.use1 || passwd_config.useapr1)
1.1 jsing 174: badopt = 1;
175: #endif
176:
177: if (badopt) {
1.3 jsing 178: passwd_usage();
179: goto err;
180: }
1.1 jsing 181:
1.3 jsing 182: if ((out = BIO_new(BIO_s_file())) == NULL)
1.1 jsing 183: goto err;
1.3 jsing 184: BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
185:
186: if (passwd_config.infile != NULL || passwd_config.in_stdin) {
187: if ((in = BIO_new(BIO_s_file())) == NULL)
1.1 jsing 188: goto err;
1.3 jsing 189: if (passwd_config.infile != NULL) {
190: assert(passwd_config.in_stdin == 0);
191: if (BIO_read_filename(in, passwd_config.infile) <= 0)
1.1 jsing 192: goto err;
193: } else {
1.3 jsing 194: assert(passwd_config.in_stdin);
1.1 jsing 195: BIO_set_fp(in, stdin, BIO_NOCLOSE);
196: }
197: }
1.3 jsing 198: if (passwd_config.usecrypt)
1.1 jsing 199: pw_maxlen = 8;
1.3 jsing 200: else if (passwd_config.use1 || passwd_config.useapr1)
1.1 jsing 201: pw_maxlen = 256;/* arbitrary limit, should be enough for most
202: * passwords */
203:
204: if (passwds == NULL) {
205: /* no passwords on the command line */
206:
207: passwd_malloc_size = pw_maxlen + 2;
208: /* longer than necessary so that we can warn about truncation */
209: passwd = passwd_malloc = malloc(passwd_malloc_size);
210: if (passwd_malloc == NULL)
211: goto err;
212: }
1.3 jsing 213: if (in == NULL && passwds == NULL) {
1.1 jsing 214: /* build a null-terminated list */
215: static char *passwds_static[2] = {NULL, NULL};
216:
217: passwds = passwds_static;
218: if (in == NULL)
1.3 jsing 219: if (EVP_read_pw_string(passwd_malloc,
220: passwd_malloc_size, "Password: ",
221: !(passed_salt || passwd_config.noverify)) != 0)
1.1 jsing 222: goto err;
223: passwds[0] = passwd_malloc;
224: }
225: if (in == NULL) {
226: assert(passwds != NULL);
227: assert(*passwds != NULL);
228:
1.3 jsing 229: do { /* loop over list of passwords */
1.1 jsing 230: passwd = *passwds++;
1.3 jsing 231: if (!do_passwd(passed_salt, &passwd_config.salt,
232: &salt_malloc, passwd, out, passwd_config.quiet,
233: passwd_config.table, passwd_config.reverse,
234: pw_maxlen, passwd_config.usecrypt,
235: passwd_config.use1, passwd_config.useapr1))
1.1 jsing 236: goto err;
1.3 jsing 237: } while (*passwds != NULL);
238: } else {
1.1 jsing 239: int done;
240:
241: assert(passwd != NULL);
242: do {
243: int r = BIO_gets(in, passwd, pw_maxlen + 1);
244: if (r > 0) {
245: char *c = (strchr(passwd, '\n'));
246: if (c != NULL)
247: *c = 0; /* truncate at newline */
248: else {
249: /* ignore rest of line */
250: char trash[BUFSIZ];
251: do
252: r = BIO_gets(in, trash, sizeof trash);
253: while ((r > 0) && (!strchr(trash, '\n')));
254: }
255:
1.3 jsing 256: if (!do_passwd(passed_salt, &passwd_config.salt,
257: &salt_malloc, passwd, out,
258: passwd_config.quiet, passwd_config.table,
259: passwd_config.reverse, pw_maxlen,
260: passwd_config.usecrypt, passwd_config.use1,
261: passwd_config.useapr1))
1.1 jsing 262: goto err;
263: }
264: done = (r <= 0);
1.3 jsing 265: } while (!done);
1.1 jsing 266: }
267: ret = 0;
268:
269: err:
270: ERR_print_errors(bio_err);
1.3 jsing 271:
1.1 jsing 272: free(salt_malloc);
273: free(passwd_malloc);
1.3 jsing 274:
1.1 jsing 275: BIO_free(in);
1.3 jsing 276: BIO_free_all(out);
1.1 jsing 277:
278: return (ret);
279: }
280:
281:
282: #ifndef NO_MD5CRYPT_1
283: /* MD5-based password algorithm (should probably be available as a library
284: * function; then the static buffer would not be acceptable).
285: * For magic string "1", this should be compatible to the MD5-based BSD
286: * password algorithm.
287: * For 'magic' string "apr1", this is compatible to the MD5-based Apache
288: * password algorithm.
289: * (Apparently, the Apache password algorithm is identical except that the
290: * 'magic' string was changed -- the laziest application of the NIH principle
291: * I've ever encountered.)
292: */
293: static char *
294: md5crypt(const char *passwd, const char *magic, const char *salt)
295: {
296: static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5h
297: * ash..........\0" */
298: unsigned char buf[MD5_DIGEST_LENGTH];
299: char *salt_out;
300: int n;
301: unsigned int i;
302: EVP_MD_CTX md, md2;
303: size_t passwd_len, salt_len;
304:
305: passwd_len = strlen(passwd);
306: out_buf[0] = '$';
307: out_buf[1] = 0;
308: assert(strlen(magic) <= 4); /* "1" or "apr1" */
309: strlcat(out_buf, magic, sizeof(out_buf));
310: strlcat(out_buf, "$", sizeof(out_buf));
311: strlcat(out_buf, salt, sizeof(out_buf));
312: assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
313: salt_out = out_buf + 2 + strlen(magic);
314: salt_len = strlen(salt_out);
315: assert(salt_len <= 8);
316:
317: EVP_MD_CTX_init(&md);
318: EVP_DigestInit_ex(&md, EVP_md5(), NULL);
319: EVP_DigestUpdate(&md, passwd, passwd_len);
320: EVP_DigestUpdate(&md, "$", 1);
321: EVP_DigestUpdate(&md, magic, strlen(magic));
322: EVP_DigestUpdate(&md, "$", 1);
323: EVP_DigestUpdate(&md, salt_out, salt_len);
324:
325: EVP_MD_CTX_init(&md2);
326: EVP_DigestInit_ex(&md2, EVP_md5(), NULL);
327: EVP_DigestUpdate(&md2, passwd, passwd_len);
328: EVP_DigestUpdate(&md2, salt_out, salt_len);
329: EVP_DigestUpdate(&md2, passwd, passwd_len);
330: EVP_DigestFinal_ex(&md2, buf, NULL);
331:
332: for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
333: EVP_DigestUpdate(&md, buf, sizeof buf);
334: EVP_DigestUpdate(&md, buf, i);
335:
336: n = passwd_len;
337: while (n) {
338: EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
339: n >>= 1;
340: }
341: EVP_DigestFinal_ex(&md, buf, NULL);
342:
343: for (i = 0; i < 1000; i++) {
344: EVP_DigestInit_ex(&md2, EVP_md5(), NULL);
345: EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf,
346: (i & 1) ? passwd_len : sizeof buf);
347: if (i % 3)
348: EVP_DigestUpdate(&md2, salt_out, salt_len);
349: if (i % 7)
350: EVP_DigestUpdate(&md2, passwd, passwd_len);
351: EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd,
352: (i & 1) ? sizeof buf : passwd_len);
353: EVP_DigestFinal_ex(&md2, buf, NULL);
354: }
355: EVP_MD_CTX_cleanup(&md2);
356:
357: {
358: /* transform buf into output string */
359:
360: unsigned char buf_perm[sizeof buf];
361: int dest, source;
362: char *output;
363:
364: /* silly output permutation */
365: for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
366: buf_perm[dest] = buf[source];
367: buf_perm[14] = buf[5];
368: buf_perm[15] = buf[11];
369: assert(16 == sizeof buf_perm);
370:
371: output = salt_out + salt_len;
372: assert(output == out_buf + strlen(out_buf));
373:
374: *output++ = '$';
375:
376: for (i = 0; i < 15; i += 3) {
377: *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
378: *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
379: (buf_perm[i + 2] >> 6)];
380: *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
381: (buf_perm[i + 1] >> 4)];
382: *output++ = cov_2char[buf_perm[i] >> 2];
383: }
384: assert(i == 15);
385: *output++ = cov_2char[buf_perm[i] & 0x3f];
386: *output++ = cov_2char[buf_perm[i] >> 6];
387: *output = 0;
388: assert(strlen(out_buf) < sizeof(out_buf));
389: }
390: EVP_MD_CTX_cleanup(&md);
391:
392: return out_buf;
393: }
394: #endif
395:
396:
397: static int
398: do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
399: char *passwd, BIO * out, int quiet, int table, int reverse,
400: size_t pw_maxlen, int usecrypt, int use1, int useapr1)
401: {
402: char *hash = NULL;
403:
404: assert(salt_p != NULL);
405: assert(salt_malloc_p != NULL);
406:
407: /* first make sure we have a salt */
408: if (!passed_salt) {
409: #ifndef OPENSSL_NO_DES
410: if (usecrypt) {
411: if (*salt_malloc_p == NULL) {
412: *salt_p = *salt_malloc_p = malloc(3);
413: if (*salt_malloc_p == NULL)
414: goto err;
415: }
1.2 jsing 416: arc4random_buf(*salt_p, 2);
1.1 jsing 417: (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
418: (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
419: (*salt_p)[2] = 0;
420: }
421: #endif /* !OPENSSL_NO_DES */
422:
423: #ifndef NO_MD5CRYPT_1
424: if (use1 || useapr1) {
425: int i;
426:
427: if (*salt_malloc_p == NULL) {
428: *salt_p = *salt_malloc_p = malloc(9);
429: if (*salt_malloc_p == NULL)
430: goto err;
431: }
1.2 jsing 432: arc4random_buf(*salt_p, 8);
1.1 jsing 433:
434: for (i = 0; i < 8; i++)
435: (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
436: (*salt_p)[8] = 0;
437: }
438: #endif /* !NO_MD5CRYPT_1 */
439: }
440: assert(*salt_p != NULL);
441:
442: /* truncate password if necessary */
443: if ((strlen(passwd) > pw_maxlen)) {
444: if (!quiet)
445: /*
446: * XXX: really we should know how to print a size_t,
447: * not cast it
448: */
449: BIO_printf(bio_err, "Warning: truncating password to %u characters\n", (unsigned) pw_maxlen);
450: passwd[pw_maxlen] = 0;
451: }
452: assert(strlen(passwd) <= pw_maxlen);
453:
454: /* now compute password hash */
455: #ifndef OPENSSL_NO_DES
456: if (usecrypt)
457: hash = DES_crypt(passwd, *salt_p);
458: #endif
459: #ifndef NO_MD5CRYPT_1
460: if (use1 || useapr1)
461: hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
462: #endif
463: assert(hash != NULL);
464:
465: if (table && !reverse)
466: BIO_printf(out, "%s\t%s\n", passwd, hash);
467: else if (table && reverse)
468: BIO_printf(out, "%s\t%s\n", hash, passwd);
469: else
470: BIO_printf(out, "%s\n", hash);
471: return 1;
472:
473: err:
474: return 0;
475: }
476: #else
477:
478: int
479: passwd_main(int argc, char **argv)
480: {
481: fputs("Program not available.\n", stderr)
482: return (1);
483: }
484: #endif