passwd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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
  8. */
  9. #include <string.h>
  10. #include "apps.h"
  11. #include "progs.h"
  12. #include <openssl/bio.h>
  13. #include <openssl/err.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/rand.h>
  16. #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  17. # include <openssl/des.h>
  18. #endif
  19. #include <openssl/md5.h>
  20. #include <openssl/sha.h>
  21. static unsigned const char cov_2char[64] = {
  22. /* from crypto/des/fcrypt.c */
  23. 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
  24. 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
  25. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
  26. 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
  27. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62,
  28. 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  29. 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
  30. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
  31. };
  32. static const char ascii_dollar[] = { 0x24, 0x00 };
  33. typedef enum {
  34. passwd_unset = 0,
  35. passwd_md5,
  36. passwd_apr1,
  37. passwd_sha256,
  38. passwd_sha512,
  39. passwd_aixmd5
  40. } passwd_modes;
  41. static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
  42. char *passwd, BIO *out, int quiet, int table,
  43. int reverse, size_t pw_maxlen, passwd_modes mode);
  44. typedef enum OPTION_choice {
  45. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  46. OPT_IN,
  47. OPT_NOVERIFY, OPT_QUIET, OPT_TABLE, OPT_REVERSE, OPT_APR1,
  48. OPT_1, OPT_5, OPT_6, OPT_AIXMD5, OPT_SALT, OPT_STDIN,
  49. OPT_R_ENUM, OPT_PROV_ENUM
  50. } OPTION_CHOICE;
  51. const OPTIONS passwd_options[] = {
  52. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [password]\n"},
  53. OPT_SECTION("General"),
  54. {"help", OPT_HELP, '-', "Display this summary"},
  55. OPT_SECTION("Input"),
  56. {"in", OPT_IN, '<', "Read passwords from file"},
  57. {"noverify", OPT_NOVERIFY, '-',
  58. "Never verify when reading password from terminal"},
  59. {"stdin", OPT_STDIN, '-', "Read passwords from stdin"},
  60. OPT_SECTION("Output"),
  61. {"quiet", OPT_QUIET, '-', "No warnings"},
  62. {"table", OPT_TABLE, '-', "Format output as table"},
  63. {"reverse", OPT_REVERSE, '-', "Switch table columns"},
  64. OPT_SECTION("Cryptographic"),
  65. {"salt", OPT_SALT, 's', "Use provided salt"},
  66. {"6", OPT_6, '-', "SHA512-based password algorithm"},
  67. {"5", OPT_5, '-', "SHA256-based password algorithm"},
  68. {"apr1", OPT_APR1, '-', "MD5-based password algorithm, Apache variant"},
  69. {"1", OPT_1, '-', "MD5-based password algorithm"},
  70. {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
  71. OPT_R_OPTIONS,
  72. OPT_PROV_OPTIONS,
  73. OPT_PARAMETERS(),
  74. {"password", 0, 0, "Password text to digest (optional)"},
  75. {NULL}
  76. };
  77. int passwd_main(int argc, char **argv)
  78. {
  79. BIO *in = NULL;
  80. char *infile = NULL, *salt = NULL, *passwd = NULL, **passwds = NULL;
  81. char *salt_malloc = NULL, *passwd_malloc = NULL, *prog;
  82. OPTION_CHOICE o;
  83. int in_stdin = 0, pw_source_defined = 0;
  84. #ifndef OPENSSL_NO_UI_CONSOLE
  85. int in_noverify = 0;
  86. #endif
  87. int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
  88. int ret = 1;
  89. passwd_modes mode = passwd_unset;
  90. size_t passwd_malloc_size = 0;
  91. size_t pw_maxlen = 256; /* arbitrary limit, should be enough for most
  92. * passwords */
  93. prog = opt_init(argc, argv, passwd_options);
  94. while ((o = opt_next()) != OPT_EOF) {
  95. switch (o) {
  96. case OPT_EOF:
  97. case OPT_ERR:
  98. opthelp:
  99. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  100. goto end;
  101. case OPT_HELP:
  102. opt_help(passwd_options);
  103. ret = 0;
  104. goto end;
  105. case OPT_IN:
  106. if (pw_source_defined)
  107. goto opthelp;
  108. infile = opt_arg();
  109. pw_source_defined = 1;
  110. break;
  111. case OPT_NOVERIFY:
  112. #ifndef OPENSSL_NO_UI_CONSOLE
  113. in_noverify = 1;
  114. #endif
  115. break;
  116. case OPT_QUIET:
  117. quiet = 1;
  118. break;
  119. case OPT_TABLE:
  120. table = 1;
  121. break;
  122. case OPT_REVERSE:
  123. reverse = 1;
  124. break;
  125. case OPT_1:
  126. if (mode != passwd_unset)
  127. goto opthelp;
  128. mode = passwd_md5;
  129. break;
  130. case OPT_5:
  131. if (mode != passwd_unset)
  132. goto opthelp;
  133. mode = passwd_sha256;
  134. break;
  135. case OPT_6:
  136. if (mode != passwd_unset)
  137. goto opthelp;
  138. mode = passwd_sha512;
  139. break;
  140. case OPT_APR1:
  141. if (mode != passwd_unset)
  142. goto opthelp;
  143. mode = passwd_apr1;
  144. break;
  145. case OPT_AIXMD5:
  146. if (mode != passwd_unset)
  147. goto opthelp;
  148. mode = passwd_aixmd5;
  149. break;
  150. case OPT_SALT:
  151. passed_salt = 1;
  152. salt = opt_arg();
  153. break;
  154. case OPT_STDIN:
  155. if (pw_source_defined)
  156. goto opthelp;
  157. in_stdin = 1;
  158. pw_source_defined = 1;
  159. break;
  160. case OPT_R_CASES:
  161. if (!opt_rand(o))
  162. goto end;
  163. break;
  164. case OPT_PROV_CASES:
  165. if (!opt_provider(o))
  166. goto end;
  167. break;
  168. }
  169. }
  170. /* All remaining arguments are the password text */
  171. argc = opt_num_rest();
  172. argv = opt_rest();
  173. if (*argv != NULL) {
  174. if (pw_source_defined)
  175. goto opthelp;
  176. pw_source_defined = 1;
  177. passwds = argv;
  178. }
  179. if (mode == passwd_unset) {
  180. /* use default */
  181. mode = passwd_md5;
  182. }
  183. if (infile != NULL && in_stdin) {
  184. BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog);
  185. goto end;
  186. }
  187. if (infile != NULL || in_stdin) {
  188. /*
  189. * If in_stdin is true, we know that infile is NULL, and that
  190. * bio_open_default() will give us back an alias for stdin.
  191. */
  192. in = bio_open_default(infile, 'r', FORMAT_TEXT);
  193. if (in == NULL)
  194. goto end;
  195. }
  196. if (passwds == NULL) {
  197. /* no passwords on the command line */
  198. passwd_malloc_size = pw_maxlen + 2;
  199. /* longer than necessary so that we can warn about truncation */
  200. passwd = passwd_malloc =
  201. app_malloc(passwd_malloc_size, "password buffer");
  202. }
  203. if ((in == NULL) && (passwds == NULL)) {
  204. /*
  205. * we use the following method to make sure what
  206. * in the 'else' section is always compiled, to
  207. * avoid rot of not-frequently-used code.
  208. */
  209. if (1) {
  210. #ifndef OPENSSL_NO_UI_CONSOLE
  211. /* build a null-terminated list */
  212. static char *passwds_static[2] = { NULL, NULL };
  213. passwds = passwds_static;
  214. if (in == NULL) {
  215. if (EVP_read_pw_string
  216. (passwd_malloc, passwd_malloc_size, "Password: ",
  217. !(passed_salt || in_noverify)) != 0)
  218. goto end;
  219. }
  220. passwds[0] = passwd_malloc;
  221. } else {
  222. #endif
  223. BIO_printf(bio_err, "password required\n");
  224. goto end;
  225. }
  226. }
  227. if (in == NULL) {
  228. assert(passwds != NULL);
  229. assert(*passwds != NULL);
  230. do { /* loop over list of passwords */
  231. passwd = *passwds++;
  232. if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
  233. quiet, table, reverse, pw_maxlen, mode))
  234. goto end;
  235. } while (*passwds != NULL);
  236. } else {
  237. /* in != NULL */
  238. int done;
  239. assert(passwd != NULL);
  240. do {
  241. int r = BIO_gets(in, passwd, pw_maxlen + 1);
  242. if (r > 0) {
  243. char *c = (strchr(passwd, '\n'));
  244. if (c != NULL) {
  245. *c = 0; /* truncate at newline */
  246. } else {
  247. /* ignore rest of line */
  248. char trash[BUFSIZ];
  249. do
  250. r = BIO_gets(in, trash, sizeof(trash));
  251. while ((r > 0) && (!strchr(trash, '\n')));
  252. }
  253. if (!do_passwd
  254. (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
  255. table, reverse, pw_maxlen, mode))
  256. goto end;
  257. }
  258. done = (r <= 0);
  259. } while (!done);
  260. }
  261. ret = 0;
  262. end:
  263. #if 0
  264. ERR_print_errors(bio_err);
  265. #endif
  266. OPENSSL_free(salt_malloc);
  267. OPENSSL_free(passwd_malloc);
  268. BIO_free(in);
  269. return ret;
  270. }
  271. /*
  272. * MD5-based password algorithm (should probably be available as a library
  273. * function; then the static buffer would not be acceptable). For magic
  274. * string "1", this should be compatible to the MD5-based BSD password
  275. * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
  276. * Apache password algorithm. (Apparently, the Apache password algorithm is
  277. * identical except that the 'magic' string was changed -- the laziest
  278. * application of the NIH principle I've ever encountered.)
  279. */
  280. static char *md5crypt(const char *passwd, const char *magic, const char *salt)
  281. {
  282. /* "$apr1$..salt..$.......md5hash..........\0" */
  283. static char out_buf[6 + 9 + 24 + 2];
  284. unsigned char buf[MD5_DIGEST_LENGTH];
  285. char ascii_magic[5]; /* "apr1" plus '\0' */
  286. char ascii_salt[9]; /* Max 8 chars plus '\0' */
  287. char *ascii_passwd = NULL;
  288. char *salt_out;
  289. int n;
  290. unsigned int i;
  291. EVP_MD_CTX *md = NULL, *md2 = NULL;
  292. size_t passwd_len, salt_len, magic_len;
  293. passwd_len = strlen(passwd);
  294. out_buf[0] = 0;
  295. magic_len = strlen(magic);
  296. OPENSSL_strlcpy(ascii_magic, magic, sizeof(ascii_magic));
  297. #ifdef CHARSET_EBCDIC
  298. if ((magic[0] & 0x80) != 0) /* High bit is 1 in EBCDIC alnums */
  299. ebcdic2ascii(ascii_magic, ascii_magic, magic_len);
  300. #endif
  301. /* The salt gets truncated to 8 chars */
  302. OPENSSL_strlcpy(ascii_salt, salt, sizeof(ascii_salt));
  303. salt_len = strlen(ascii_salt);
  304. #ifdef CHARSET_EBCDIC
  305. ebcdic2ascii(ascii_salt, ascii_salt, salt_len);
  306. #endif
  307. #ifdef CHARSET_EBCDIC
  308. ascii_passwd = OPENSSL_strdup(passwd);
  309. if (ascii_passwd == NULL)
  310. return NULL;
  311. ebcdic2ascii(ascii_passwd, ascii_passwd, passwd_len);
  312. passwd = ascii_passwd;
  313. #endif
  314. if (magic_len > 0) {
  315. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  316. if (magic_len > 4) /* assert it's "1" or "apr1" */
  317. goto err;
  318. OPENSSL_strlcat(out_buf, ascii_magic, sizeof(out_buf));
  319. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  320. }
  321. OPENSSL_strlcat(out_buf, ascii_salt, sizeof(out_buf));
  322. if (strlen(out_buf) > 6 + 8) /* assert "$apr1$..salt.." */
  323. goto err;
  324. salt_out = out_buf;
  325. if (magic_len > 0)
  326. salt_out += 2 + magic_len;
  327. if (salt_len > 8)
  328. goto err;
  329. md = EVP_MD_CTX_new();
  330. if (md == NULL
  331. || !EVP_DigestInit_ex(md, EVP_md5(), NULL)
  332. || !EVP_DigestUpdate(md, passwd, passwd_len))
  333. goto err;
  334. if (magic_len > 0)
  335. if (!EVP_DigestUpdate(md, ascii_dollar, 1)
  336. || !EVP_DigestUpdate(md, ascii_magic, magic_len)
  337. || !EVP_DigestUpdate(md, ascii_dollar, 1))
  338. goto err;
  339. if (!EVP_DigestUpdate(md, ascii_salt, salt_len))
  340. goto err;
  341. md2 = EVP_MD_CTX_new();
  342. if (md2 == NULL
  343. || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
  344. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  345. || !EVP_DigestUpdate(md2, ascii_salt, salt_len)
  346. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  347. || !EVP_DigestFinal_ex(md2, buf, NULL))
  348. goto err;
  349. for (i = passwd_len; i > sizeof(buf); i -= sizeof(buf)) {
  350. if (!EVP_DigestUpdate(md, buf, sizeof(buf)))
  351. goto err;
  352. }
  353. if (!EVP_DigestUpdate(md, buf, i))
  354. goto err;
  355. n = passwd_len;
  356. while (n) {
  357. if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
  358. goto err;
  359. n >>= 1;
  360. }
  361. if (!EVP_DigestFinal_ex(md, buf, NULL))
  362. return NULL;
  363. for (i = 0; i < 1000; i++) {
  364. if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
  365. goto err;
  366. if (!EVP_DigestUpdate(md2,
  367. (i & 1) ? (unsigned const char *)passwd : buf,
  368. (i & 1) ? passwd_len : sizeof(buf)))
  369. goto err;
  370. if (i % 3) {
  371. if (!EVP_DigestUpdate(md2, ascii_salt, salt_len))
  372. goto err;
  373. }
  374. if (i % 7) {
  375. if (!EVP_DigestUpdate(md2, passwd, passwd_len))
  376. goto err;
  377. }
  378. if (!EVP_DigestUpdate(md2,
  379. (i & 1) ? buf : (unsigned const char *)passwd,
  380. (i & 1) ? sizeof(buf) : passwd_len))
  381. goto err;
  382. if (!EVP_DigestFinal_ex(md2, buf, NULL))
  383. goto err;
  384. }
  385. EVP_MD_CTX_free(md2);
  386. EVP_MD_CTX_free(md);
  387. md2 = NULL;
  388. md = NULL;
  389. {
  390. /* transform buf into output string */
  391. unsigned char buf_perm[sizeof(buf)];
  392. int dest, source;
  393. char *output;
  394. /* silly output permutation */
  395. for (dest = 0, source = 0; dest < 14;
  396. dest++, source = (source + 6) % 17)
  397. buf_perm[dest] = buf[source];
  398. buf_perm[14] = buf[5];
  399. buf_perm[15] = buf[11];
  400. # ifndef PEDANTIC /* Unfortunately, this generates a "no
  401. * effect" warning */
  402. assert(16 == sizeof(buf_perm));
  403. # endif
  404. output = salt_out + salt_len;
  405. assert(output == out_buf + strlen(out_buf));
  406. *output++ = ascii_dollar[0];
  407. for (i = 0; i < 15; i += 3) {
  408. *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
  409. *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
  410. (buf_perm[i + 2] >> 6)];
  411. *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
  412. (buf_perm[i + 1] >> 4)];
  413. *output++ = cov_2char[buf_perm[i] >> 2];
  414. }
  415. assert(i == 15);
  416. *output++ = cov_2char[buf_perm[i] & 0x3f];
  417. *output++ = cov_2char[buf_perm[i] >> 6];
  418. *output = 0;
  419. assert(strlen(out_buf) < sizeof(out_buf));
  420. #ifdef CHARSET_EBCDIC
  421. ascii2ebcdic(out_buf, out_buf, strlen(out_buf));
  422. #endif
  423. }
  424. return out_buf;
  425. err:
  426. OPENSSL_free(ascii_passwd);
  427. EVP_MD_CTX_free(md2);
  428. EVP_MD_CTX_free(md);
  429. return NULL;
  430. }
  431. /*
  432. * SHA based password algorithm, describe by Ulrich Drepper here:
  433. * https://www.akkadia.org/drepper/SHA-crypt.txt
  434. * (note that it's in the public domain)
  435. */
  436. static char *shacrypt(const char *passwd, const char *magic, const char *salt)
  437. {
  438. /* Prefix for optional rounds specification. */
  439. static const char rounds_prefix[] = "rounds=";
  440. /* Maximum salt string length. */
  441. # define SALT_LEN_MAX 16
  442. /* Default number of rounds if not explicitly specified. */
  443. # define ROUNDS_DEFAULT 5000
  444. /* Minimum number of rounds. */
  445. # define ROUNDS_MIN 1000
  446. /* Maximum number of rounds. */
  447. # define ROUNDS_MAX 999999999
  448. /* "$6$rounds=<N>$......salt......$...shahash(up to 86 chars)...\0" */
  449. static char out_buf[3 + 17 + 17 + 86 + 1];
  450. unsigned char buf[SHA512_DIGEST_LENGTH];
  451. unsigned char temp_buf[SHA512_DIGEST_LENGTH];
  452. size_t buf_size = 0;
  453. char ascii_magic[2];
  454. char ascii_salt[17]; /* Max 16 chars plus '\0' */
  455. char *ascii_passwd = NULL;
  456. size_t n;
  457. EVP_MD_CTX *md = NULL, *md2 = NULL;
  458. const EVP_MD *sha = NULL;
  459. size_t passwd_len, salt_len, magic_len;
  460. unsigned int rounds = 5000; /* Default */
  461. char rounds_custom = 0;
  462. char *p_bytes = NULL;
  463. char *s_bytes = NULL;
  464. char *cp = NULL;
  465. passwd_len = strlen(passwd);
  466. magic_len = strlen(magic);
  467. /* assert it's "5" or "6" */
  468. if (magic_len != 1)
  469. return NULL;
  470. switch (magic[0]) {
  471. case '5':
  472. sha = EVP_sha256();
  473. buf_size = 32;
  474. break;
  475. case '6':
  476. sha = EVP_sha512();
  477. buf_size = 64;
  478. break;
  479. default:
  480. return NULL;
  481. }
  482. if (strncmp(salt, rounds_prefix, sizeof(rounds_prefix) - 1) == 0) {
  483. const char *num = salt + sizeof(rounds_prefix) - 1;
  484. char *endp;
  485. unsigned long int srounds = strtoul (num, &endp, 10);
  486. if (*endp == '$') {
  487. salt = endp + 1;
  488. if (srounds > ROUNDS_MAX)
  489. rounds = ROUNDS_MAX;
  490. else if (srounds < ROUNDS_MIN)
  491. rounds = ROUNDS_MIN;
  492. else
  493. rounds = (unsigned int)srounds;
  494. rounds_custom = 1;
  495. } else {
  496. return NULL;
  497. }
  498. }
  499. OPENSSL_strlcpy(ascii_magic, magic, sizeof(ascii_magic));
  500. #ifdef CHARSET_EBCDIC
  501. if ((magic[0] & 0x80) != 0) /* High bit is 1 in EBCDIC alnums */
  502. ebcdic2ascii(ascii_magic, ascii_magic, magic_len);
  503. #endif
  504. /* The salt gets truncated to 16 chars */
  505. OPENSSL_strlcpy(ascii_salt, salt, sizeof(ascii_salt));
  506. salt_len = strlen(ascii_salt);
  507. #ifdef CHARSET_EBCDIC
  508. ebcdic2ascii(ascii_salt, ascii_salt, salt_len);
  509. #endif
  510. #ifdef CHARSET_EBCDIC
  511. ascii_passwd = OPENSSL_strdup(passwd);
  512. if (ascii_passwd == NULL)
  513. return NULL;
  514. ebcdic2ascii(ascii_passwd, ascii_passwd, passwd_len);
  515. passwd = ascii_passwd;
  516. #endif
  517. out_buf[0] = 0;
  518. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  519. OPENSSL_strlcat(out_buf, ascii_magic, sizeof(out_buf));
  520. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  521. if (rounds_custom) {
  522. char tmp_buf[80]; /* "rounds=999999999" */
  523. sprintf(tmp_buf, "rounds=%u", rounds);
  524. #ifdef CHARSET_EBCDIC
  525. /* In case we're really on a ASCII based platform and just pretend */
  526. if (tmp_buf[0] != 0x72) /* ASCII 'r' */
  527. ebcdic2ascii(tmp_buf, tmp_buf, strlen(tmp_buf));
  528. #endif
  529. OPENSSL_strlcat(out_buf, tmp_buf, sizeof(out_buf));
  530. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  531. }
  532. OPENSSL_strlcat(out_buf, ascii_salt, sizeof(out_buf));
  533. /* assert "$5$rounds=999999999$......salt......" */
  534. if (strlen(out_buf) > 3 + 17 * rounds_custom + salt_len )
  535. goto err;
  536. md = EVP_MD_CTX_new();
  537. if (md == NULL
  538. || !EVP_DigestInit_ex(md, sha, NULL)
  539. || !EVP_DigestUpdate(md, passwd, passwd_len)
  540. || !EVP_DigestUpdate(md, ascii_salt, salt_len))
  541. goto err;
  542. md2 = EVP_MD_CTX_new();
  543. if (md2 == NULL
  544. || !EVP_DigestInit_ex(md2, sha, NULL)
  545. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  546. || !EVP_DigestUpdate(md2, ascii_salt, salt_len)
  547. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  548. || !EVP_DigestFinal_ex(md2, buf, NULL))
  549. goto err;
  550. for (n = passwd_len; n > buf_size; n -= buf_size) {
  551. if (!EVP_DigestUpdate(md, buf, buf_size))
  552. goto err;
  553. }
  554. if (!EVP_DigestUpdate(md, buf, n))
  555. goto err;
  556. n = passwd_len;
  557. while (n) {
  558. if (!EVP_DigestUpdate(md,
  559. (n & 1) ? buf : (unsigned const char *)passwd,
  560. (n & 1) ? buf_size : passwd_len))
  561. goto err;
  562. n >>= 1;
  563. }
  564. if (!EVP_DigestFinal_ex(md, buf, NULL))
  565. return NULL;
  566. /* P sequence */
  567. if (!EVP_DigestInit_ex(md2, sha, NULL))
  568. goto err;
  569. for (n = passwd_len; n > 0; n--)
  570. if (!EVP_DigestUpdate(md2, passwd, passwd_len))
  571. goto err;
  572. if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
  573. return NULL;
  574. if ((p_bytes = OPENSSL_zalloc(passwd_len)) == NULL)
  575. goto err;
  576. for (cp = p_bytes, n = passwd_len; n > buf_size; n -= buf_size, cp += buf_size)
  577. memcpy(cp, temp_buf, buf_size);
  578. memcpy(cp, temp_buf, n);
  579. /* S sequence */
  580. if (!EVP_DigestInit_ex(md2, sha, NULL))
  581. goto err;
  582. for (n = 16 + buf[0]; n > 0; n--)
  583. if (!EVP_DigestUpdate(md2, ascii_salt, salt_len))
  584. goto err;
  585. if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
  586. return NULL;
  587. if ((s_bytes = OPENSSL_zalloc(salt_len)) == NULL)
  588. goto err;
  589. for (cp = s_bytes, n = salt_len; n > buf_size; n -= buf_size, cp += buf_size)
  590. memcpy(cp, temp_buf, buf_size);
  591. memcpy(cp, temp_buf, n);
  592. for (n = 0; n < rounds; n++) {
  593. if (!EVP_DigestInit_ex(md2, sha, NULL))
  594. goto err;
  595. if (!EVP_DigestUpdate(md2,
  596. (n & 1) ? (unsigned const char *)p_bytes : buf,
  597. (n & 1) ? passwd_len : buf_size))
  598. goto err;
  599. if (n % 3) {
  600. if (!EVP_DigestUpdate(md2, s_bytes, salt_len))
  601. goto err;
  602. }
  603. if (n % 7) {
  604. if (!EVP_DigestUpdate(md2, p_bytes, passwd_len))
  605. goto err;
  606. }
  607. if (!EVP_DigestUpdate(md2,
  608. (n & 1) ? buf : (unsigned const char *)p_bytes,
  609. (n & 1) ? buf_size : passwd_len))
  610. goto err;
  611. if (!EVP_DigestFinal_ex(md2, buf, NULL))
  612. goto err;
  613. }
  614. EVP_MD_CTX_free(md2);
  615. EVP_MD_CTX_free(md);
  616. md2 = NULL;
  617. md = NULL;
  618. OPENSSL_free(p_bytes);
  619. OPENSSL_free(s_bytes);
  620. p_bytes = NULL;
  621. s_bytes = NULL;
  622. cp = out_buf + strlen(out_buf);
  623. *cp++ = ascii_dollar[0];
  624. # define b64_from_24bit(B2, B1, B0, N) \
  625. do { \
  626. unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
  627. int i = (N); \
  628. while (i-- > 0) \
  629. { \
  630. *cp++ = cov_2char[w & 0x3f]; \
  631. w >>= 6; \
  632. } \
  633. } while (0)
  634. switch (magic[0]) {
  635. case '5':
  636. b64_from_24bit (buf[0], buf[10], buf[20], 4);
  637. b64_from_24bit (buf[21], buf[1], buf[11], 4);
  638. b64_from_24bit (buf[12], buf[22], buf[2], 4);
  639. b64_from_24bit (buf[3], buf[13], buf[23], 4);
  640. b64_from_24bit (buf[24], buf[4], buf[14], 4);
  641. b64_from_24bit (buf[15], buf[25], buf[5], 4);
  642. b64_from_24bit (buf[6], buf[16], buf[26], 4);
  643. b64_from_24bit (buf[27], buf[7], buf[17], 4);
  644. b64_from_24bit (buf[18], buf[28], buf[8], 4);
  645. b64_from_24bit (buf[9], buf[19], buf[29], 4);
  646. b64_from_24bit (0, buf[31], buf[30], 3);
  647. break;
  648. case '6':
  649. b64_from_24bit (buf[0], buf[21], buf[42], 4);
  650. b64_from_24bit (buf[22], buf[43], buf[1], 4);
  651. b64_from_24bit (buf[44], buf[2], buf[23], 4);
  652. b64_from_24bit (buf[3], buf[24], buf[45], 4);
  653. b64_from_24bit (buf[25], buf[46], buf[4], 4);
  654. b64_from_24bit (buf[47], buf[5], buf[26], 4);
  655. b64_from_24bit (buf[6], buf[27], buf[48], 4);
  656. b64_from_24bit (buf[28], buf[49], buf[7], 4);
  657. b64_from_24bit (buf[50], buf[8], buf[29], 4);
  658. b64_from_24bit (buf[9], buf[30], buf[51], 4);
  659. b64_from_24bit (buf[31], buf[52], buf[10], 4);
  660. b64_from_24bit (buf[53], buf[11], buf[32], 4);
  661. b64_from_24bit (buf[12], buf[33], buf[54], 4);
  662. b64_from_24bit (buf[34], buf[55], buf[13], 4);
  663. b64_from_24bit (buf[56], buf[14], buf[35], 4);
  664. b64_from_24bit (buf[15], buf[36], buf[57], 4);
  665. b64_from_24bit (buf[37], buf[58], buf[16], 4);
  666. b64_from_24bit (buf[59], buf[17], buf[38], 4);
  667. b64_from_24bit (buf[18], buf[39], buf[60], 4);
  668. b64_from_24bit (buf[40], buf[61], buf[19], 4);
  669. b64_from_24bit (buf[62], buf[20], buf[41], 4);
  670. b64_from_24bit (0, 0, buf[63], 2);
  671. break;
  672. default:
  673. goto err;
  674. }
  675. *cp = '\0';
  676. #ifdef CHARSET_EBCDIC
  677. ascii2ebcdic(out_buf, out_buf, strlen(out_buf));
  678. #endif
  679. return out_buf;
  680. err:
  681. EVP_MD_CTX_free(md2);
  682. EVP_MD_CTX_free(md);
  683. OPENSSL_free(p_bytes);
  684. OPENSSL_free(s_bytes);
  685. OPENSSL_free(ascii_passwd);
  686. return NULL;
  687. }
  688. static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
  689. char *passwd, BIO *out, int quiet, int table,
  690. int reverse, size_t pw_maxlen, passwd_modes mode)
  691. {
  692. char *hash = NULL;
  693. assert(salt_p != NULL);
  694. assert(salt_malloc_p != NULL);
  695. /* first make sure we have a salt */
  696. if (!passed_salt) {
  697. size_t saltlen = 0;
  698. size_t i;
  699. if (mode == passwd_md5 || mode == passwd_apr1 || mode == passwd_aixmd5)
  700. saltlen = 8;
  701. if (mode == passwd_sha256 || mode == passwd_sha512)
  702. saltlen = 16;
  703. assert(saltlen != 0);
  704. if (*salt_malloc_p == NULL)
  705. *salt_p = *salt_malloc_p = app_malloc(saltlen + 1, "salt buffer");
  706. if (RAND_bytes((unsigned char *)*salt_p, saltlen) <= 0)
  707. goto end;
  708. for (i = 0; i < saltlen; i++)
  709. (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
  710. (*salt_p)[i] = 0;
  711. # ifdef CHARSET_EBCDIC
  712. /* The password encryption function will convert back to ASCII */
  713. ascii2ebcdic(*salt_p, *salt_p, saltlen);
  714. # endif
  715. }
  716. assert(*salt_p != NULL);
  717. /* truncate password if necessary */
  718. if ((strlen(passwd) > pw_maxlen)) {
  719. if (!quiet)
  720. /*
  721. * XXX: really we should know how to print a size_t, not cast it
  722. */
  723. BIO_printf(bio_err,
  724. "Warning: truncating password to %u characters\n",
  725. (unsigned)pw_maxlen);
  726. passwd[pw_maxlen] = 0;
  727. }
  728. assert(strlen(passwd) <= pw_maxlen);
  729. /* now compute password hash */
  730. if (mode == passwd_md5 || mode == passwd_apr1)
  731. hash = md5crypt(passwd, (mode == passwd_md5 ? "1" : "apr1"), *salt_p);
  732. if (mode == passwd_aixmd5)
  733. hash = md5crypt(passwd, "", *salt_p);
  734. if (mode == passwd_sha256 || mode == passwd_sha512)
  735. hash = shacrypt(passwd, (mode == passwd_sha256 ? "5" : "6"), *salt_p);
  736. assert(hash != NULL);
  737. if (table && !reverse)
  738. BIO_printf(out, "%s\t%s\n", passwd, hash);
  739. else if (table && reverse)
  740. BIO_printf(out, "%s\t%s\n", hash, passwd);
  741. else
  742. BIO_printf(out, "%s\n", hash);
  743. return 1;
  744. end:
  745. return 0;
  746. }