3 #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
7 #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
18 #ifndef OPENSSL_NO_DES
27 #define PROG passwd_main
30 static unsigned const char cov_2char[64]={
32 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
33 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
34 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
35 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
36 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
37 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
38 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
39 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
42 static int do_passwd(
int passed_salt,
char **salt_p,
char **salt_malloc_p,
43 char *passwd,
BIO *out,
int quiet,
int table,
int reverse,
44 size_t pw_maxlen,
int usecrypt,
int use1,
int useapr1);
58 int MAIN(
int,
char **);
60 int MAIN(
int argc,
char **argv)
66 char *salt = NULL, *passwd = NULL, **passwds = NULL;
67 char *salt_malloc = NULL, *passwd_malloc = NULL;
68 size_t passwd_malloc_size = 0;
69 int pw_source_defined = 0;
71 int i, badopt, opt_done;
72 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
73 int usecrypt = 0, use1 = 0, useapr1 = 0;
88 #ifdef OPENSSL_SYS_VMS
95 badopt = 0, opt_done = 0;
97 while (!badopt && !opt_done && argv[++i] != NULL)
99 if (strcmp(argv[i],
"-crypt") == 0)
101 else if (strcmp(argv[i],
"-1") == 0)
103 else if (strcmp(argv[i],
"-apr1") == 0)
105 else if (strcmp(argv[i],
"-salt") == 0)
107 if ((argv[i+1] != NULL) && (salt == NULL))
115 else if (strcmp(argv[i],
"-in") == 0)
117 if ((argv[i+1] != NULL) && !pw_source_defined)
119 pw_source_defined = 1;
125 else if (strcmp(argv[i],
"-stdin") == 0)
127 if (!pw_source_defined)
129 pw_source_defined = 1;
135 else if (strcmp(argv[i],
"-noverify") == 0)
137 else if (strcmp(argv[i],
"-quiet") == 0)
139 else if (strcmp(argv[i],
"-table") == 0)
141 else if (strcmp(argv[i],
"-reverse") == 0)
143 else if (argv[i][0] ==
'-')
145 else if (!pw_source_defined)
148 pw_source_defined = 1;
156 if (!usecrypt && !use1 && !useapr1)
158 if (usecrypt + use1 + useapr1 > 1)
162 #ifdef OPENSSL_NO_DES
163 if (usecrypt) badopt = 1;
166 if (use1 || useapr1) badopt = 1;
173 #ifndef OPENSSL_NO_DES
176 #ifndef NO_MD5CRYPT_1
183 BIO_printf(
bio_err,
"-noverify never verify when reading password from terminal\n");
191 if ((infile != NULL) || in_stdin)
198 assert(in_stdin == 0);
211 else if (use1 || useapr1)
218 passwd_malloc_size = pw_maxlen + 2;
221 if (passwd_malloc == NULL)
225 if ((in == NULL) && (passwds == NULL))
228 static char *passwds_static[2] = {NULL, NULL};
230 passwds = passwds_static;
232 if (
EVP_read_pw_string(passwd_malloc, passwd_malloc_size,
"Password: ", !(passed_salt || in_noverify)) != 0)
234 passwds[0] = passwd_malloc;
239 assert(passwds != NULL);
240 assert(*passwds != NULL);
245 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
246 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
249 while (*passwds != NULL);
256 assert (passwd != NULL);
259 int r =
BIO_gets(in, passwd, pw_maxlen + 1);
262 char *
c = (strchr(passwd,
'\n')) ;
270 r =
BIO_gets(in, trash,
sizeof trash);
271 while ((r > 0) && (!strchr(trash,
'\n')));
274 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
275 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
299 #ifndef NO_MD5CRYPT_1
310 static char *md5crypt(
const char *passwd,
const char *magic,
const char *salt)
312 static char out_buf[6 + 9 + 24 + 2];
318 size_t passwd_len, salt_len;
320 passwd_len = strlen(passwd);
323 assert(strlen(magic) <= 4);
324 strncat(out_buf, magic, 4);
325 strncat(out_buf,
"$", 1);
326 strncat(out_buf, salt, 8);
327 assert(strlen(out_buf) <= 6 + 8);
328 salt_out = out_buf + 2 + strlen(magic);
329 salt_len = strlen(salt_out);
330 assert(salt_len <= 8);
347 for (i = passwd_len; i >
sizeof buf; i -=
sizeof buf)
359 for (i = 0; i < 1000; i++)
363 (i & 1) ? passwd_len :
sizeof buf);
369 (i & 1) ?
sizeof buf : passwd_len);
377 unsigned char buf_perm[
sizeof buf];
382 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
383 buf_perm[dest] = buf[source];
384 buf_perm[14] = buf[5];
385 buf_perm[15] = buf[11];
387 assert(16 ==
sizeof buf_perm);
390 output = salt_out + salt_len;
391 assert(output == out_buf + strlen(out_buf));
395 for (i = 0; i < 15; i += 3)
397 *output++ = cov_2char[buf_perm[i+2] & 0x3f];
398 *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
399 (buf_perm[i+2] >> 6)];
400 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
401 (buf_perm[i+1] >> 4)];
402 *output++ = cov_2char[buf_perm[i] >> 2];
405 *output++ = cov_2char[buf_perm[i] & 0x3f];
406 *output++ = cov_2char[buf_perm[i] >> 6];
408 assert(strlen(out_buf) <
sizeof(out_buf));
417 static int do_passwd(
int passed_salt,
char **salt_p,
char **salt_malloc_p,
418 char *passwd,
BIO *out,
int quiet,
int table,
int reverse,
419 size_t pw_maxlen,
int usecrypt,
int use1,
int useapr1)
423 assert(salt_p != NULL);
424 assert(salt_malloc_p != NULL);
429 #ifndef OPENSSL_NO_DES
432 if (*salt_malloc_p == NULL)
435 if (*salt_malloc_p == NULL)
440 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f];
441 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f];
443 #ifdef CHARSET_EBCDIC
450 #ifndef NO_MD5CRYPT_1
455 if (*salt_malloc_p == NULL)
458 if (*salt_malloc_p == NULL)
464 for (i = 0; i < 8; i++)
465 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f];
471 assert(*salt_p != NULL);
474 if ((strlen(passwd) > pw_maxlen))
478 BIO_printf(
bio_err,
"Warning: truncating password to %u characters\n", (
unsigned)pw_maxlen);
479 passwd[pw_maxlen] = 0;
481 assert(strlen(passwd) <= pw_maxlen);
484 #ifndef OPENSSL_NO_DES
488 #ifndef NO_MD5CRYPT_1
490 hash = md5crypt(passwd, (use1 ?
"1" :
"apr1"), *salt_p);
492 assert(hash != NULL);
494 if (table && !reverse)
496 else if (table && reverse)
507 int MAIN(
int argc,
char **argv)
509 fputs(
"Program not available.\n", stderr)