2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
11 # define NO_MD5CRYPT_1
14 #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/evp.h>
23 # include <openssl/rand.h>
24 # ifndef OPENSSL_NO_DES
25 # include <openssl/des.h>
27 # ifndef NO_MD5CRYPT_1
28 # include <openssl/md5.h>
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,
45 int reverse, size_t pw_maxlen, int usecrypt, int use1,
48 typedef enum OPTION_choice {
49 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
51 OPT_NOVERIFY, OPT_QUIET, OPT_TABLE, OPT_REVERSE, OPT_APR1,
52 OPT_1, OPT_CRYPT, OPT_SALT, OPT_STDIN
55 OPTIONS passwd_options[] = {
56 {"help", OPT_HELP, '-', "Display this summary"},
57 {"in", OPT_IN, '<', "Read passwords from file"},
58 {"noverify", OPT_NOVERIFY, '-',
59 "Never verify when reading password from terminal"},
60 {"quiet", OPT_QUIET, '-', "No warnings"},
61 {"table", OPT_TABLE, '-', "Format output as table"},
62 {"reverse", OPT_REVERSE, '-', "Switch table columns"},
63 {"salt", OPT_SALT, 's', "Use provided salt"},
64 {"stdin", OPT_STDIN, '-', "Read passwords from stdin"},
65 # ifndef NO_MD5CRYPT_1
66 {"apr1", OPT_APR1, '-', "MD5-based password algorithm, Apache variant"},
67 {"1", OPT_1, '-', "MD5-based password algorithm"},
69 # ifndef OPENSSL_NO_DES
70 {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
75 int passwd_main(int argc, char **argv)
78 char *infile = NULL, *salt = NULL, *passwd = NULL, **passwds = NULL;
79 char *salt_malloc = NULL, *passwd_malloc = NULL, *prog;
81 int in_stdin = 0, pw_source_defined = 0;
82 # ifndef OPENSSL_NO_UI
85 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
86 int ret = 1, usecrypt = 0, use1 = 0, useapr1 = 0;
87 size_t passwd_malloc_size = 0, pw_maxlen = 256;
89 prog = opt_init(argc, argv, passwd_options);
90 while ((o = opt_next()) != OPT_EOF) {
95 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
98 opt_help(passwd_options);
102 if (pw_source_defined)
105 pw_source_defined = 1;
108 # ifndef OPENSSL_NO_UI
135 if (pw_source_defined)
138 pw_source_defined = 1;
142 argc = opt_num_rest();
146 if (pw_source_defined)
148 pw_source_defined = 1;
152 if (!usecrypt && !use1 && !useapr1) {
156 if (usecrypt + use1 + useapr1 > 1) {
161 # ifdef OPENSSL_NO_DES
165 # ifdef NO_MD5CRYPT_1
170 if (infile != NULL && in_stdin) {
171 BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog);
175 if (infile != NULL || in_stdin) {
177 * If in_stdin is true, we know that infile is NULL, and that
178 * bio_open_default() will give us back an alias for stdin.
180 in = bio_open_default(infile, 'r', FORMAT_TEXT);
187 else if (use1 || useapr1)
188 pw_maxlen = 256; /* arbitrary limit, should be enough for most
191 if (passwds == NULL) {
192 /* no passwords on the command line */
194 passwd_malloc_size = pw_maxlen + 2;
195 /* longer than necessary so that we can warn about truncation */
196 passwd = passwd_malloc =
197 app_malloc(passwd_malloc_size, "password buffer");
200 if ((in == NULL) && (passwds == NULL)) {
202 * we use the following method to make sure what
203 * in the 'else' section is always compiled, to
204 * avoid rot of not-frequently-used code.
207 # ifndef OPENSSL_NO_UI
208 /* build a null-terminated list */
209 static char *passwds_static[2] = { NULL, NULL };
211 passwds = passwds_static;
213 if (EVP_read_pw_string
214 (passwd_malloc, passwd_malloc_size, "Password: ",
215 !(passed_salt || in_noverify)) != 0)
218 passwds[0] = passwd_malloc;
221 BIO_printf(bio_err, "password required\n");
227 assert(passwds != NULL);
228 assert(*passwds != NULL);
230 do { /* loop over list of passwords */
232 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
233 quiet, table, reverse, pw_maxlen, usecrypt, use1,
236 } while (*passwds != NULL);
241 assert(passwd != NULL);
243 int r = BIO_gets(in, passwd, pw_maxlen + 1);
245 char *c = (strchr(passwd, '\n'));
247 *c = 0; /* truncate at newline */
249 /* ignore rest of line */
252 r = BIO_gets(in, trash, sizeof(trash));
253 while ((r > 0) && (!strchr(trash, '\n')));
257 (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
258 table, reverse, pw_maxlen, usecrypt, use1, useapr1))
267 ERR_print_errors(bio_err);
268 OPENSSL_free(salt_malloc);
269 OPENSSL_free(passwd_malloc);
274 # ifndef NO_MD5CRYPT_1
276 * MD5-based password algorithm (should probably be available as a library
277 * function; then the static buffer would not be acceptable). For magic
278 * string "1", this should be compatible to the MD5-based BSD password
279 * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
280 * Apache password algorithm. (Apparently, the Apache password algorithm is
281 * identical except that the 'magic' string was changed -- the laziest
282 * application of the NIH principle I've ever encountered.)
284 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
286 /* "$apr1$..salt..$.......md5hash..........\0" */
287 static char out_buf[6 + 9 + 24 + 2];
288 unsigned char buf[MD5_DIGEST_LENGTH];
292 EVP_MD_CTX *md = NULL, *md2 = NULL;
293 size_t passwd_len, salt_len, magic_len;
295 passwd_len = strlen(passwd);
298 magic_len = strlen(magic);
300 if (magic_len > 4) /* assert it's "1" or "apr1" */
303 OPENSSL_strlcat(out_buf, magic, sizeof(out_buf));
304 OPENSSL_strlcat(out_buf, "$", sizeof(out_buf));
305 OPENSSL_strlcat(out_buf, salt, sizeof(out_buf));
307 if (strlen(out_buf) > 6 + 8) /* assert "$apr1$..salt.." */
310 salt_out = out_buf + 2 + magic_len;
311 salt_len = strlen(salt_out);
316 md = EVP_MD_CTX_new();
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, magic_len)
322 || !EVP_DigestUpdate(md, "$", 1)
323 || !EVP_DigestUpdate(md, salt_out, salt_len))
326 md2 = EVP_MD_CTX_new();
328 || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
329 || !EVP_DigestUpdate(md2, passwd, passwd_len)
330 || !EVP_DigestUpdate(md2, salt_out, salt_len)
331 || !EVP_DigestUpdate(md2, passwd, passwd_len)
332 || !EVP_DigestFinal_ex(md2, buf, NULL))
335 for (i = passwd_len; i > sizeof(buf); i -= sizeof(buf)) {
336 if (!EVP_DigestUpdate(md, buf, sizeof(buf)))
339 if (!EVP_DigestUpdate(md, buf, i))
344 if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
348 if (!EVP_DigestFinal_ex(md, buf, NULL))
351 for (i = 0; i < 1000; i++) {
352 if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
354 if (!EVP_DigestUpdate(md2,
355 (i & 1) ? (unsigned const char *)passwd : buf,
356 (i & 1) ? passwd_len : sizeof(buf)))
359 if (!EVP_DigestUpdate(md2, salt_out, salt_len))
363 if (!EVP_DigestUpdate(md2, passwd, passwd_len))
366 if (!EVP_DigestUpdate(md2,
367 (i & 1) ? buf : (unsigned const char *)passwd,
368 (i & 1) ? sizeof(buf) : passwd_len))
370 if (!EVP_DigestFinal_ex(md2, buf, NULL))
373 EVP_MD_CTX_free(md2);
379 /* transform buf into output string */
380 unsigned char buf_perm[sizeof(buf)];
384 /* silly output permutation */
385 for (dest = 0, source = 0; dest < 14;
386 dest++, source = (source + 6) % 17)
387 buf_perm[dest] = buf[source];
388 buf_perm[14] = buf[5];
389 buf_perm[15] = buf[11];
390 # ifndef PEDANTIC /* Unfortunately, this generates a "no
392 assert(16 == sizeof(buf_perm));
395 output = salt_out + salt_len;
396 assert(output == out_buf + strlen(out_buf));
400 for (i = 0; i < 15; i += 3) {
401 *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
402 *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
403 (buf_perm[i + 2] >> 6)];
404 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
405 (buf_perm[i + 1] >> 4)];
406 *output++ = cov_2char[buf_perm[i] >> 2];
409 *output++ = cov_2char[buf_perm[i] & 0x3f];
410 *output++ = cov_2char[buf_perm[i] >> 6];
412 assert(strlen(out_buf) < sizeof(out_buf));
418 EVP_MD_CTX_free(md2);
424 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
425 char *passwd, BIO *out, int quiet, int table,
426 int reverse, size_t pw_maxlen, int usecrypt, int use1,
431 assert(salt_p != NULL);
432 assert(salt_malloc_p != NULL);
434 /* first make sure we have a salt */
436 # ifndef OPENSSL_NO_DES
438 if (*salt_malloc_p == NULL)
439 *salt_p = *salt_malloc_p = app_malloc(3, "salt buffer");
440 if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
442 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
443 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
445 # ifdef CHARSET_EBCDIC
446 ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert back
450 # endif /* !OPENSSL_NO_DES */
452 # ifndef NO_MD5CRYPT_1
453 if (use1 || useapr1) {
456 if (*salt_malloc_p == NULL)
457 *salt_p = *salt_malloc_p = app_malloc(9, "salt buffer");
458 if (RAND_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 * XXX: really we should know how to print a size_t, not cast it
477 "Warning: truncating password to %u characters\n",
478 (unsigned)pw_maxlen);
479 passwd[pw_maxlen] = 0;
481 assert(strlen(passwd) <= pw_maxlen);
483 /* now compute password hash */
484 # ifndef OPENSSL_NO_DES
486 hash = DES_crypt(passwd, *salt_p);
488 # ifndef NO_MD5CRYPT_1
490 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
492 assert(hash != NULL);
494 if (table && !reverse)
495 BIO_printf(out, "%s\t%s\n", passwd, hash);
496 else if (table && reverse)
497 BIO_printf(out, "%s\t%s\n", hash, passwd);
499 BIO_printf(out, "%s\n", hash);
507 int passwd_main(int argc, char **argv)
509 BIO_printf(bio_err, "Program not available.\n");