3 #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
7 #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/rand.h>
19 #ifndef OPENSSL_NO_DES
20 # include <openssl/des.h>
23 # include <openssl/md5.h>
28 #define PROG passwd_main
31 static unsigned const char cov_2char[64]={
32 /* from crypto/des/fcrypt.c */
33 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
34 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
35 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
36 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
37 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
38 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
39 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
40 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
43 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
44 char *passwd, BIO *out, int quiet, int table, int reverse,
45 size_t pw_maxlen, int usecrypt, int use1, int useapr1);
47 /* -crypt - standard Unix password algorithm (default)
48 * -1 - MD5-based password algorithm
49 * -apr1 - MD5-based password algorithm, Apache variant
51 * -in file - read passwords from file
52 * -stdin - read passwords from stdin
53 * -noverify - never verify when reading password from terminal
54 * -quiet - no warnings
55 * -table - format output as table
56 * -reverse - switch table columns
59 int MAIN(int, char **);
61 int MAIN(int argc, char **argv)
67 char *salt = NULL, *passwd = NULL, **passwds = NULL;
68 char *salt_malloc = NULL, *passwd_malloc = NULL;
69 size_t passwd_malloc_size = 0;
70 int pw_source_defined = 0;
71 BIO *in = NULL, *out = NULL;
72 int i, badopt, opt_done;
73 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
74 int usecrypt = 0, use1 = 0, useapr1 = 0;
80 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
81 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
82 out = BIO_new(BIO_s_file());
85 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
86 #ifdef OPENSSL_SYS_VMS
88 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
89 out = BIO_push(tmpbio, out);
93 badopt = 0, opt_done = 0;
95 while (!badopt && !opt_done && argv[++i] != NULL)
97 if (strcmp(argv[i], "-crypt") == 0)
99 else if (strcmp(argv[i], "-1") == 0)
101 else if (strcmp(argv[i], "-apr1") == 0)
103 else if (strcmp(argv[i], "-salt") == 0)
105 if ((argv[i+1] != NULL) && (salt == NULL))
113 else if (strcmp(argv[i], "-in") == 0)
115 if ((argv[i+1] != NULL) && !pw_source_defined)
117 pw_source_defined = 1;
123 else if (strcmp(argv[i], "-stdin") == 0)
125 if (!pw_source_defined)
127 pw_source_defined = 1;
133 else if (strcmp(argv[i], "-noverify") == 0)
135 else if (strcmp(argv[i], "-quiet") == 0)
137 else if (strcmp(argv[i], "-table") == 0)
139 else if (strcmp(argv[i], "-reverse") == 0)
141 else if (argv[i][0] == '-')
143 else if (!pw_source_defined)
144 /* non-option arguments, use as passwords */
146 pw_source_defined = 1;
154 if (!usecrypt && !use1 && !useapr1) /* use default */
156 if (usecrypt + use1 + useapr1 > 1) /* conflict */
159 /* reject unsupported algorithms */
160 #ifdef OPENSSL_NO_DES
161 if (usecrypt) badopt = 1;
164 if (use1 || useapr1) badopt = 1;
169 BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
170 BIO_printf(bio_err, "where options are\n");
171 #ifndef OPENSSL_NO_DES
172 BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n");
174 #ifndef NO_MD5CRYPT_1
175 BIO_printf(bio_err, "-1 MD5-based password algorithm\n");
176 BIO_printf(bio_err, "-apr1 MD5-based password algorithm, Apache variant\n");
178 BIO_printf(bio_err, "-salt string use provided salt\n");
179 BIO_printf(bio_err, "-in file read passwords from file\n");
180 BIO_printf(bio_err, "-stdin read passwords from stdin\n");
181 BIO_printf(bio_err, "-noverify never verify when reading password from terminal\n");
182 BIO_printf(bio_err, "-quiet no warnings\n");
183 BIO_printf(bio_err, "-table format output as table\n");
184 BIO_printf(bio_err, "-reverse switch table columns\n");
189 if ((infile != NULL) || in_stdin)
191 in = BIO_new(BIO_s_file());
196 assert(in_stdin == 0);
197 if (BIO_read_filename(in, infile) <= 0)
203 BIO_set_fp(in, stdin, BIO_NOCLOSE);
209 else if (use1 || useapr1)
210 pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */
214 /* no passwords on the command line */
216 passwd_malloc_size = pw_maxlen + 2;
217 /* longer than necessary so that we can warn about truncation */
218 passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
219 if (passwd_malloc == NULL)
223 if ((in == NULL) && (passwds == NULL))
225 /* build a null-terminated list */
226 static char *passwds_static[2] = {NULL, NULL};
228 passwds = passwds_static;
230 if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", !(passed_salt || in_noverify)) != 0)
232 passwds[0] = passwd_malloc;
237 assert(passwds != NULL);
238 assert(*passwds != NULL);
240 do /* loop over list of passwords */
243 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
244 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
247 while (*passwds != NULL);
254 assert (passwd != NULL);
257 int r = BIO_gets(in, passwd, pw_maxlen + 1);
260 char *c = (strchr(passwd, '\n')) ;
262 *c = 0; /* truncate at newline */
265 /* ignore rest of line */
268 r = BIO_gets(in, trash, sizeof trash);
269 while ((r > 0) && (!strchr(trash, '\n')));
272 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
273 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
283 ERR_print_errors(bio_err);
285 OPENSSL_free(salt_malloc);
287 OPENSSL_free(passwd_malloc);
296 #ifndef NO_MD5CRYPT_1
297 /* MD5-based password algorithm (should probably be available as a library
298 * function; then the static buffer would not be acceptable).
299 * For magic string "1", this should be compatible to the MD5-based BSD
300 * password algorithm.
301 * For 'magic' string "apr1", this is compatible to the MD5-based Apache
302 * password algorithm.
303 * (Apparently, the Apache password algorithm is identical except that the
304 * 'magic' string was changed -- the laziest application of the NIH principle
305 * I've ever encountered.)
307 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
309 static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
310 unsigned char buf[MD5_DIGEST_LENGTH];
314 size_t passwd_len, salt_len;
316 passwd_len = strlen(passwd);
319 assert(strlen(magic) <= 4); /* "1" or "apr1" */
320 strncat(out_buf, magic, 4);
321 strncat(out_buf, "$", 1);
322 strncat(out_buf, salt, 8);
323 assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
324 salt_out = out_buf + 2 + strlen(magic);
325 salt_len = strlen(salt_out);
326 assert(salt_len <= 8);
329 MD5_Update(&md, passwd, passwd_len);
330 MD5_Update(&md, "$", 1);
331 MD5_Update(&md, magic, strlen(magic));
332 MD5_Update(&md, "$", 1);
333 MD5_Update(&md, salt_out, salt_len);
339 MD5_Update(&md2, passwd, passwd_len);
340 MD5_Update(&md2, salt_out, salt_len);
341 MD5_Update(&md2, passwd, passwd_len);
342 MD5_Final(buf, &md2);
344 for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
345 MD5_Update(&md, buf, sizeof buf);
346 MD5_Update(&md, buf, i);
351 MD5_Update(&md, (n & 1) ? "\0" : passwd, 1);
356 for (i = 0; i < 1000; i++)
361 MD5_Update(&md2, (i & 1) ? (unsigned char *) passwd : buf,
362 (i & 1) ? passwd_len : sizeof buf);
364 MD5_Update(&md2, salt_out, salt_len);
366 MD5_Update(&md2, passwd, passwd_len);
367 MD5_Update(&md2, (i & 1) ? buf : (unsigned char *) passwd,
368 (i & 1) ? sizeof buf : passwd_len);
369 MD5_Final(buf, &md2);
373 /* transform buf into output string */
375 unsigned char buf_perm[sizeof buf];
379 /* silly output permutation */
380 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
381 buf_perm[dest] = buf[source];
382 buf_perm[14] = buf[5];
383 buf_perm[15] = buf[11];
384 #ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
385 assert(16 == sizeof buf_perm);
388 output = salt_out + salt_len;
389 assert(output == out_buf + strlen(out_buf));
393 for (i = 0; i < 15; i += 3)
395 *output++ = cov_2char[buf_perm[i+2] & 0x3f];
396 *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
397 (buf_perm[i+2] >> 6)];
398 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
399 (buf_perm[i+1] >> 4)];
400 *output++ = cov_2char[buf_perm[i] >> 2];
403 *output++ = cov_2char[buf_perm[i] & 0x3f];
404 *output++ = cov_2char[buf_perm[i] >> 6];
406 assert(strlen(out_buf) < sizeof(out_buf));
414 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
415 char *passwd, BIO *out, int quiet, int table, int reverse,
416 size_t pw_maxlen, int usecrypt, int use1, int useapr1)
420 assert(salt_p != NULL);
421 assert(salt_malloc_p != NULL);
423 /* first make sure we have a salt */
426 #ifndef OPENSSL_NO_DES
429 if (*salt_malloc_p == NULL)
431 *salt_p = *salt_malloc_p = OPENSSL_malloc(3);
432 if (*salt_malloc_p == NULL)
435 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
437 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
438 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
440 #ifdef CHARSET_EBCDIC
441 ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
445 #endif /* !OPENSSL_NO_DES */
447 #ifndef NO_MD5CRYPT_1
452 if (*salt_malloc_p == NULL)
454 *salt_p = *salt_malloc_p = OPENSSL_malloc(9);
455 if (*salt_malloc_p == NULL)
458 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
461 for (i = 0; i < 8; i++)
462 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
465 #endif /* !NO_MD5CRYPT_1 */
468 assert(*salt_p != NULL);
470 /* truncate password if necessary */
471 if ((strlen(passwd) > pw_maxlen))
474 BIO_printf(bio_err, "Warning: truncating password to %u characters\n", pw_maxlen);
475 passwd[pw_maxlen] = 0;
477 assert(strlen(passwd) <= pw_maxlen);
479 /* now compute password hash */
480 #ifndef OPENSSL_NO_DES
482 hash = des_crypt(passwd, *salt_p);
484 #ifndef NO_MD5CRYPT_1
486 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
488 assert(hash != NULL);
490 if (table && !reverse)
491 BIO_printf(out, "%s\t%s\n", passwd, hash);
492 else if (table && reverse)
493 BIO_printf(out, "%s\t%s\n", hash, passwd);
495 BIO_printf(out, "%s\n", hash);
503 int MAIN(int argc, char **argv)
505 fputs("Program not available.\n", stderr)