sl@0: /* apps/passwd.c */ sl@0: sl@0: #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC sl@0: # define NO_MD5CRYPT_1 sl@0: #endif sl@0: sl@0: #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1) sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include "apps.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_DES sl@0: # include sl@0: #endif sl@0: #ifndef NO_MD5CRYPT_1 sl@0: # include sl@0: #endif sl@0: sl@0: sl@0: #undef PROG sl@0: #define PROG passwd_main sl@0: sl@0: sl@0: static unsigned const char cov_2char[64]={ sl@0: /* from crypto/des/fcrypt.c */ sl@0: 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35, sl@0: 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44, sl@0: 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C, sl@0: 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54, sl@0: 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62, sl@0: 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A, sl@0: 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72, sl@0: 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A sl@0: }; sl@0: sl@0: static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p, sl@0: char *passwd, BIO *out, int quiet, int table, int reverse, sl@0: size_t pw_maxlen, int usecrypt, int use1, int useapr1); sl@0: sl@0: /* -crypt - standard Unix password algorithm (default) sl@0: * -1 - MD5-based password algorithm sl@0: * -apr1 - MD5-based password algorithm, Apache variant sl@0: * -salt string - salt sl@0: * -in file - read passwords from file sl@0: * -stdin - read passwords from stdin sl@0: * -noverify - never verify when reading password from terminal sl@0: * -quiet - no warnings sl@0: * -table - format output as table sl@0: * -reverse - switch table columns sl@0: */ sl@0: sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: int ret = 1; sl@0: char *infile = NULL; sl@0: int in_stdin = 0; sl@0: int in_noverify = 0; sl@0: char *salt = NULL, *passwd = NULL, **passwds = NULL; sl@0: char *salt_malloc = NULL, *passwd_malloc = NULL; sl@0: size_t passwd_malloc_size = 0; sl@0: int pw_source_defined = 0; sl@0: BIO *in = NULL, *out = NULL; sl@0: int i, badopt, opt_done; sl@0: int passed_salt = 0, quiet = 0, table = 0, reverse = 0; sl@0: int usecrypt = 0, use1 = 0, useapr1 = 0; sl@0: size_t pw_maxlen = 0; sl@0: sl@0: apps_startup(); sl@0: sl@0: if (bio_err == NULL) sl@0: if ((bio_err=BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto err; sl@0: out = BIO_new(BIO_s_file()); sl@0: if (out == NULL) sl@0: goto err; sl@0: BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: out = BIO_push(tmpbio, out); sl@0: } sl@0: #endif sl@0: sl@0: badopt = 0, opt_done = 0; sl@0: i = 0; sl@0: while (!badopt && !opt_done && argv[++i] != NULL) sl@0: { sl@0: if (strcmp(argv[i], "-crypt") == 0) sl@0: usecrypt = 1; sl@0: else if (strcmp(argv[i], "-1") == 0) sl@0: use1 = 1; sl@0: else if (strcmp(argv[i], "-apr1") == 0) sl@0: useapr1 = 1; sl@0: else if (strcmp(argv[i], "-salt") == 0) sl@0: { sl@0: if ((argv[i+1] != NULL) && (salt == NULL)) sl@0: { sl@0: passed_salt = 1; sl@0: salt = argv[++i]; sl@0: } sl@0: else sl@0: badopt = 1; sl@0: } sl@0: else if (strcmp(argv[i], "-in") == 0) sl@0: { sl@0: if ((argv[i+1] != NULL) && !pw_source_defined) sl@0: { sl@0: pw_source_defined = 1; sl@0: infile = argv[++i]; sl@0: } sl@0: else sl@0: badopt = 1; sl@0: } sl@0: else if (strcmp(argv[i], "-stdin") == 0) sl@0: { sl@0: if (!pw_source_defined) sl@0: { sl@0: pw_source_defined = 1; sl@0: in_stdin = 1; sl@0: } sl@0: else sl@0: badopt = 1; sl@0: } sl@0: else if (strcmp(argv[i], "-noverify") == 0) sl@0: in_noverify = 1; sl@0: else if (strcmp(argv[i], "-quiet") == 0) sl@0: quiet = 1; sl@0: else if (strcmp(argv[i], "-table") == 0) sl@0: table = 1; sl@0: else if (strcmp(argv[i], "-reverse") == 0) sl@0: reverse = 1; sl@0: else if (argv[i][0] == '-') sl@0: badopt = 1; sl@0: else if (!pw_source_defined) sl@0: /* non-option arguments, use as passwords */ sl@0: { sl@0: pw_source_defined = 1; sl@0: passwds = &argv[i]; sl@0: opt_done = 1; sl@0: } sl@0: else sl@0: badopt = 1; sl@0: } sl@0: sl@0: if (!usecrypt && !use1 && !useapr1) /* use default */ sl@0: usecrypt = 1; sl@0: if (usecrypt + use1 + useapr1 > 1) /* conflict */ sl@0: badopt = 1; sl@0: sl@0: /* reject unsupported algorithms */ sl@0: #ifdef OPENSSL_NO_DES sl@0: if (usecrypt) badopt = 1; sl@0: #endif sl@0: #ifdef NO_MD5CRYPT_1 sl@0: if (use1 || useapr1) badopt = 1; sl@0: #endif sl@0: sl@0: if (badopt) sl@0: { sl@0: BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n"); sl@0: BIO_printf(bio_err, "where options are\n"); sl@0: #ifndef OPENSSL_NO_DES sl@0: BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n"); sl@0: #endif sl@0: #ifndef NO_MD5CRYPT_1 sl@0: BIO_printf(bio_err, "-1 MD5-based password algorithm\n"); sl@0: BIO_printf(bio_err, "-apr1 MD5-based password algorithm, Apache variant\n"); sl@0: #endif sl@0: BIO_printf(bio_err, "-salt string use provided salt\n"); sl@0: BIO_printf(bio_err, "-in file read passwords from file\n"); sl@0: BIO_printf(bio_err, "-stdin read passwords from stdin\n"); sl@0: BIO_printf(bio_err, "-noverify never verify when reading password from terminal\n"); sl@0: BIO_printf(bio_err, "-quiet no warnings\n"); sl@0: BIO_printf(bio_err, "-table format output as table\n"); sl@0: BIO_printf(bio_err, "-reverse switch table columns\n"); sl@0: sl@0: goto err; sl@0: } sl@0: sl@0: if ((infile != NULL) || in_stdin) sl@0: { sl@0: in = BIO_new(BIO_s_file()); sl@0: if (in == NULL) sl@0: goto err; sl@0: if (infile != NULL) sl@0: { sl@0: assert(in_stdin == 0); sl@0: if (BIO_read_filename(in, infile) <= 0) sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: assert(in_stdin); sl@0: BIO_set_fp(in, stdin, BIO_NOCLOSE); sl@0: sl@0: } sl@0: } sl@0: sl@0: if (usecrypt) sl@0: pw_maxlen = 8; sl@0: else if (use1 || useapr1) sl@0: pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */ sl@0: sl@0: if (passwds == NULL) sl@0: { sl@0: /* no passwords on the command line */ sl@0: sl@0: passwd_malloc_size = pw_maxlen + 2; sl@0: /* longer than necessary so that we can warn about truncation */ sl@0: passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size); sl@0: if (passwd_malloc == NULL) sl@0: goto err; sl@0: } sl@0: sl@0: if ((in == NULL) && (passwds == NULL)) sl@0: { sl@0: /* build a null-terminated list */ sl@0: static char *passwds_static[2] = {NULL, NULL}; sl@0: sl@0: passwds = passwds_static; sl@0: if (in == NULL) sl@0: if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", !(passed_salt || in_noverify)) != 0) sl@0: goto err; sl@0: passwds[0] = passwd_malloc; sl@0: } sl@0: sl@0: if (in == NULL) sl@0: { sl@0: assert(passwds != NULL); sl@0: assert(*passwds != NULL); sl@0: sl@0: do /* loop over list of passwords */ sl@0: { sl@0: passwd = *passwds++; sl@0: if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out, sl@0: quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1)) sl@0: goto err; sl@0: } sl@0: while (*passwds != NULL); sl@0: } sl@0: else sl@0: /* in != NULL */ sl@0: { sl@0: int done; sl@0: sl@0: assert (passwd != NULL); sl@0: do sl@0: { sl@0: int r = BIO_gets(in, passwd, pw_maxlen + 1); sl@0: if (r > 0) sl@0: { sl@0: char *c = (strchr(passwd, '\n')) ; sl@0: if (c != NULL) sl@0: *c = 0; /* truncate at newline */ sl@0: else sl@0: { sl@0: /* ignore rest of line */ sl@0: char trash[BUFSIZ]; sl@0: do sl@0: r = BIO_gets(in, trash, sizeof trash); sl@0: while ((r > 0) && (!strchr(trash, '\n'))); sl@0: } sl@0: sl@0: if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out, sl@0: quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1)) sl@0: goto err; sl@0: } sl@0: done = (r <= 0); sl@0: } sl@0: while (!done); sl@0: } sl@0: ret = 0; sl@0: sl@0: err: sl@0: ERR_print_errors(bio_err); sl@0: if (salt_malloc) sl@0: OPENSSL_free(salt_malloc); sl@0: if (passwd_malloc) sl@0: OPENSSL_free(passwd_malloc); sl@0: if (in) sl@0: BIO_free(in); sl@0: if (out) sl@0: BIO_free_all(out); sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ret); sl@0: } sl@0: sl@0: sl@0: #ifndef NO_MD5CRYPT_1 sl@0: /* MD5-based password algorithm (should probably be available as a library sl@0: * function; then the static buffer would not be acceptable). sl@0: * For magic string "1", this should be compatible to the MD5-based BSD sl@0: * password algorithm. sl@0: * For 'magic' string "apr1", this is compatible to the MD5-based Apache sl@0: * password algorithm. sl@0: * (Apparently, the Apache password algorithm is identical except that the sl@0: * 'magic' string was changed -- the laziest application of the NIH principle sl@0: * I've ever encountered.) sl@0: */ sl@0: static char *md5crypt(const char *passwd, const char *magic, const char *salt) sl@0: { sl@0: static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */ sl@0: unsigned char buf[MD5_DIGEST_LENGTH]; sl@0: char *salt_out; sl@0: int n; sl@0: unsigned int i; sl@0: EVP_MD_CTX md,md2; sl@0: size_t passwd_len, salt_len; sl@0: sl@0: passwd_len = strlen(passwd); sl@0: out_buf[0] = '$'; sl@0: out_buf[1] = 0; sl@0: assert(strlen(magic) <= 4); /* "1" or "apr1" */ sl@0: strncat(out_buf, magic, 4); sl@0: strncat(out_buf, "$", 1); sl@0: strncat(out_buf, salt, 8); sl@0: assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */ sl@0: salt_out = out_buf + 2 + strlen(magic); sl@0: salt_len = strlen(salt_out); sl@0: assert(salt_len <= 8); sl@0: sl@0: EVP_MD_CTX_init(&md); sl@0: EVP_DigestInit_ex(&md,EVP_md5(), NULL); sl@0: EVP_DigestUpdate(&md, passwd, passwd_len); sl@0: EVP_DigestUpdate(&md, "$", 1); sl@0: EVP_DigestUpdate(&md, magic, strlen(magic)); sl@0: EVP_DigestUpdate(&md, "$", 1); sl@0: EVP_DigestUpdate(&md, salt_out, salt_len); sl@0: sl@0: EVP_MD_CTX_init(&md2); sl@0: EVP_DigestInit_ex(&md2,EVP_md5(), NULL); sl@0: EVP_DigestUpdate(&md2, passwd, passwd_len); sl@0: EVP_DigestUpdate(&md2, salt_out, salt_len); sl@0: EVP_DigestUpdate(&md2, passwd, passwd_len); sl@0: EVP_DigestFinal_ex(&md2, buf, NULL); sl@0: sl@0: for (i = passwd_len; i > sizeof buf; i -= sizeof buf) sl@0: EVP_DigestUpdate(&md, buf, sizeof buf); sl@0: EVP_DigestUpdate(&md, buf, i); sl@0: sl@0: n = passwd_len; sl@0: while (n) sl@0: { sl@0: EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1); sl@0: n >>= 1; sl@0: } sl@0: EVP_DigestFinal_ex(&md, buf, NULL); sl@0: sl@0: for (i = 0; i < 1000; i++) sl@0: { sl@0: EVP_DigestInit_ex(&md2,EVP_md5(), NULL); sl@0: EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf, sl@0: (i & 1) ? passwd_len : sizeof buf); sl@0: if (i % 3) sl@0: EVP_DigestUpdate(&md2, salt_out, salt_len); sl@0: if (i % 7) sl@0: EVP_DigestUpdate(&md2, passwd, passwd_len); sl@0: EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd, sl@0: (i & 1) ? sizeof buf : passwd_len); sl@0: EVP_DigestFinal_ex(&md2, buf, NULL); sl@0: } sl@0: EVP_MD_CTX_cleanup(&md2); sl@0: sl@0: { sl@0: /* transform buf into output string */ sl@0: sl@0: unsigned char buf_perm[sizeof buf]; sl@0: int dest, source; sl@0: char *output; sl@0: sl@0: /* silly output permutation */ sl@0: for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17) sl@0: buf_perm[dest] = buf[source]; sl@0: buf_perm[14] = buf[5]; sl@0: buf_perm[15] = buf[11]; sl@0: #ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */ sl@0: assert(16 == sizeof buf_perm); sl@0: #endif sl@0: sl@0: output = salt_out + salt_len; sl@0: assert(output == out_buf + strlen(out_buf)); sl@0: sl@0: *output++ = '$'; sl@0: sl@0: for (i = 0; i < 15; i += 3) sl@0: { sl@0: *output++ = cov_2char[buf_perm[i+2] & 0x3f]; sl@0: *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) | sl@0: (buf_perm[i+2] >> 6)]; sl@0: *output++ = cov_2char[((buf_perm[i] & 3) << 4) | sl@0: (buf_perm[i+1] >> 4)]; sl@0: *output++ = cov_2char[buf_perm[i] >> 2]; sl@0: } sl@0: assert(i == 15); sl@0: *output++ = cov_2char[buf_perm[i] & 0x3f]; sl@0: *output++ = cov_2char[buf_perm[i] >> 6]; sl@0: *output = 0; sl@0: assert(strlen(out_buf) < sizeof(out_buf)); sl@0: } sl@0: EVP_MD_CTX_cleanup(&md); sl@0: sl@0: return out_buf; sl@0: } sl@0: #endif sl@0: sl@0: sl@0: static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p, sl@0: char *passwd, BIO *out, int quiet, int table, int reverse, sl@0: size_t pw_maxlen, int usecrypt, int use1, int useapr1) sl@0: { sl@0: char *hash = NULL; sl@0: sl@0: assert(salt_p != NULL); sl@0: assert(salt_malloc_p != NULL); sl@0: sl@0: /* first make sure we have a salt */ sl@0: if (!passed_salt) sl@0: { sl@0: #ifndef OPENSSL_NO_DES sl@0: if (usecrypt) sl@0: { sl@0: if (*salt_malloc_p == NULL) sl@0: { sl@0: *salt_p = *salt_malloc_p = OPENSSL_malloc(3); sl@0: if (*salt_malloc_p == NULL) sl@0: goto err; sl@0: } sl@0: if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0) sl@0: goto err; sl@0: (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */ sl@0: (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */ sl@0: (*salt_p)[2] = 0; sl@0: #ifdef CHARSET_EBCDIC sl@0: ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert sl@0: * back to ASCII */ sl@0: #endif sl@0: } sl@0: #endif /* !OPENSSL_NO_DES */ sl@0: sl@0: #ifndef NO_MD5CRYPT_1 sl@0: if (use1 || useapr1) sl@0: { sl@0: int i; sl@0: sl@0: if (*salt_malloc_p == NULL) sl@0: { sl@0: *salt_p = *salt_malloc_p = OPENSSL_malloc(9); sl@0: if (*salt_malloc_p == NULL) sl@0: goto err; sl@0: } sl@0: if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0) sl@0: goto err; sl@0: sl@0: for (i = 0; i < 8; i++) sl@0: (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */ sl@0: (*salt_p)[8] = 0; sl@0: } sl@0: #endif /* !NO_MD5CRYPT_1 */ sl@0: } sl@0: sl@0: assert(*salt_p != NULL); sl@0: sl@0: /* truncate password if necessary */ sl@0: if ((strlen(passwd) > pw_maxlen)) sl@0: { sl@0: if (!quiet) sl@0: /* XXX: really we should know how to print a size_t, not cast it */ sl@0: BIO_printf(bio_err, "Warning: truncating password to %u characters\n", (unsigned)pw_maxlen); sl@0: passwd[pw_maxlen] = 0; sl@0: } sl@0: assert(strlen(passwd) <= pw_maxlen); sl@0: sl@0: /* now compute password hash */ sl@0: #ifndef OPENSSL_NO_DES sl@0: if (usecrypt) sl@0: hash = DES_crypt(passwd, *salt_p); sl@0: #endif sl@0: #ifndef NO_MD5CRYPT_1 sl@0: if (use1 || useapr1) sl@0: hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p); sl@0: #endif sl@0: assert(hash != NULL); sl@0: sl@0: if (table && !reverse) sl@0: BIO_printf(out, "%s\t%s\n", passwd, hash); sl@0: else if (table && reverse) sl@0: BIO_printf(out, "%s\t%s\n", hash, passwd); sl@0: else sl@0: BIO_printf(out, "%s\n", hash); sl@0: return 1; sl@0: sl@0: err: sl@0: return 0; sl@0: } sl@0: #else sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: fputs("Program not available.\n", stderr) sl@0: OPENSSL_EXIT(1); sl@0: } sl@0: #endif