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, '<', "Pead 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;
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 #ifndef OPENSSL_NO_UI
203 /* build a null-terminated list */
204 static char *passwds_static[2] = { NULL, NULL };
206 passwds = passwds_static;
208 if (EVP_read_pw_string
209 (passwd_malloc, passwd_malloc_size, "Password: ",
210 !(passed_salt || in_noverify)) != 0)
212 passwds[0] = passwd_malloc;
215 BIO_printf(bio_err, "password required\n");
222 assert(passwds != NULL);
223 assert(*passwds != NULL);
225 do { /* loop over list of passwords */
227 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
228 quiet, table, reverse, pw_maxlen, usecrypt, use1,
232 while (*passwds != NULL);
238 assert(passwd != NULL);
240 int r = BIO_gets(in, passwd, pw_maxlen + 1);
242 char *c = (strchr(passwd, '\n'));
244 *c = 0; /* truncate at newline */
246 /* ignore rest of line */
249 r = BIO_gets(in, trash, sizeof trash);
250 while ((r > 0) && (!strchr(trash, '\n')));
254 (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
255 table, reverse, pw_maxlen, usecrypt, use1, useapr1))
265 ERR_print_errors(bio_err);
266 OPENSSL_free(salt_malloc);
267 OPENSSL_free(passwd_malloc);
272 # ifndef NO_MD5CRYPT_1
274 * MD5-based password algorithm (should probably be available as a library
275 * function; then the static buffer would not be acceptable). For magic
276 * string "1", this should be compatible to the MD5-based BSD password
277 * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
278 * Apache password algorithm. (Apparently, the Apache password algorithm is
279 * identical except that the 'magic' string was changed -- the laziest
280 * application of the NIH principle I've ever encountered.)
282 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
284 /* "$apr1$..salt..$.......md5hash..........\0" */
285 static char out_buf[6 + 9 + 24 + 2];
286 unsigned char buf[MD5_DIGEST_LENGTH];
290 EVP_MD_CTX *md = NULL, *md2 = NULL;
291 size_t passwd_len, salt_len;
293 passwd_len = strlen(passwd);
296 assert(strlen(magic) <= 4); /* "1" or "apr1" */
297 OPENSSL_strlcat(out_buf, magic, sizeof out_buf);
298 OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
299 OPENSSL_strlcat(out_buf, salt, sizeof out_buf);
300 assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
301 salt_out = out_buf + 2 + strlen(magic);
302 salt_len = strlen(salt_out);
303 assert(salt_len <= 8);
305 md = EVP_MD_CTX_new();
307 || !EVP_DigestInit_ex(md, EVP_md5(), NULL)
308 || !EVP_DigestUpdate(md, passwd, passwd_len)
309 || !EVP_DigestUpdate(md, "$", 1)
310 || !EVP_DigestUpdate(md, magic, strlen(magic))
311 || !EVP_DigestUpdate(md, "$", 1)
312 || !EVP_DigestUpdate(md, salt_out, salt_len))
314 md2 = EVP_MD_CTX_new();
316 || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
317 || !EVP_DigestUpdate(md2, passwd, passwd_len)
318 || !EVP_DigestUpdate(md2, salt_out, salt_len)
319 || !EVP_DigestUpdate(md2, passwd, passwd_len)
320 || !EVP_DigestFinal_ex(md2, buf, NULL))
323 for (i = passwd_len; i > sizeof buf; i -= sizeof buf) {
324 if (!EVP_DigestUpdate(md, buf, sizeof buf))
327 if (!EVP_DigestUpdate(md, buf, i))
332 if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
336 if (!EVP_DigestFinal_ex(md, buf, NULL))
339 for (i = 0; i < 1000; i++) {
340 if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
342 if (!EVP_DigestUpdate(md2,
343 (i & 1) ? (unsigned const char *)passwd : buf,
344 (i & 1) ? passwd_len : sizeof buf))
347 if (!EVP_DigestUpdate(md2, salt_out, salt_len))
351 if (!EVP_DigestUpdate(md2, passwd, passwd_len))
354 if (!EVP_DigestUpdate(md2,
355 (i & 1) ? buf : (unsigned const char *)passwd,
356 (i & 1) ? sizeof buf : passwd_len))
358 if (!EVP_DigestFinal_ex(md2, buf, NULL))
361 EVP_MD_CTX_free(md2);
367 /* transform buf into output string */
369 unsigned char buf_perm[sizeof buf];
373 /* silly output permutation */
374 for (dest = 0, source = 0; dest < 14;
375 dest++, source = (source + 6) % 17)
376 buf_perm[dest] = buf[source];
377 buf_perm[14] = buf[5];
378 buf_perm[15] = buf[11];
379 # ifndef PEDANTIC /* Unfortunately, this generates a "no
381 assert(16 == sizeof buf_perm);
384 output = salt_out + salt_len;
385 assert(output == out_buf + strlen(out_buf));
389 for (i = 0; i < 15; i += 3) {
390 *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
391 *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
392 (buf_perm[i + 2] >> 6)];
393 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
394 (buf_perm[i + 1] >> 4)];
395 *output++ = cov_2char[buf_perm[i] >> 2];
398 *output++ = cov_2char[buf_perm[i] & 0x3f];
399 *output++ = cov_2char[buf_perm[i] >> 6];
401 assert(strlen(out_buf) < sizeof(out_buf));
407 EVP_MD_CTX_free(md2);
413 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
414 char *passwd, BIO *out, int quiet, int table,
415 int reverse, size_t pw_maxlen, int usecrypt, int use1,
420 assert(salt_p != NULL);
421 assert(salt_malloc_p != NULL);
423 /* first make sure we have a salt */
425 # ifndef OPENSSL_NO_DES
427 if (*salt_malloc_p == NULL) {
428 *salt_p = *salt_malloc_p = app_malloc(3, "salt buffer");
430 if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
432 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
433 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
435 # ifdef CHARSET_EBCDIC
436 ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert back
440 # endif /* !OPENSSL_NO_DES */
442 # ifndef NO_MD5CRYPT_1
443 if (use1 || useapr1) {
446 if (*salt_malloc_p == NULL) {
447 *salt_p = *salt_malloc_p = app_malloc(9, "salt buffer");
449 if (RAND_bytes((unsigned char *)*salt_p, 8) <= 0)
452 for (i = 0; i < 8; i++)
453 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
456 # endif /* !NO_MD5CRYPT_1 */
459 assert(*salt_p != NULL);
461 /* truncate password if necessary */
462 if ((strlen(passwd) > pw_maxlen)) {
465 * XXX: really we should know how to print a size_t, not cast it
468 "Warning: truncating password to %u characters\n",
469 (unsigned)pw_maxlen);
470 passwd[pw_maxlen] = 0;
472 assert(strlen(passwd) <= pw_maxlen);
474 /* now compute password hash */
475 # ifndef OPENSSL_NO_DES
477 hash = DES_crypt(passwd, *salt_p);
479 # ifndef NO_MD5CRYPT_1
481 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
483 assert(hash != NULL);
485 if (table && !reverse)
486 BIO_printf(out, "%s\t%s\n", passwd, hash);
487 else if (table && reverse)
488 BIO_printf(out, "%s\t%s\n", hash, passwd);
490 BIO_printf(out, "%s\n", hash);
498 int passwd_main(int argc, char **argv)
500 BIO_printf(bio_err, "Program not available.\n");