passwd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. argc = opt_num_rest();
  171. argv = opt_rest();
  172. if (*argv != NULL) {
  173. if (pw_source_defined)
  174. goto opthelp;
  175. pw_source_defined = 1;
  176. passwds = argv;
  177. }
  178. if (mode == passwd_unset) {
  179. /* use default */
  180. mode = passwd_md5;
  181. }
  182. if (infile != NULL && in_stdin) {
  183. BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog);
  184. goto end;
  185. }
  186. if (infile != NULL || in_stdin) {
  187. /*
  188. * If in_stdin is true, we know that infile is NULL, and that
  189. * bio_open_default() will give us back an alias for stdin.
  190. */
  191. in = bio_open_default(infile, 'r', FORMAT_TEXT);
  192. if (in == NULL)
  193. goto end;
  194. }
  195. if (passwds == NULL) {
  196. /* no passwords on the command line */
  197. passwd_malloc_size = pw_maxlen + 2;
  198. /* longer than necessary so that we can warn about truncation */
  199. passwd = passwd_malloc =
  200. app_malloc(passwd_malloc_size, "password buffer");
  201. }
  202. if ((in == NULL) && (passwds == NULL)) {
  203. /*
  204. * we use the following method to make sure what
  205. * in the 'else' section is always compiled, to
  206. * avoid rot of not-frequently-used code.
  207. */
  208. if (1) {
  209. #ifndef OPENSSL_NO_UI_CONSOLE
  210. /* build a null-terminated list */
  211. static char *passwds_static[2] = { NULL, NULL };
  212. passwds = passwds_static;
  213. if (in == NULL) {
  214. if (EVP_read_pw_string
  215. (passwd_malloc, passwd_malloc_size, "Password: ",
  216. !(passed_salt || in_noverify)) != 0)
  217. goto end;
  218. }
  219. passwds[0] = passwd_malloc;
  220. } else {
  221. #endif
  222. BIO_printf(bio_err, "password required\n");
  223. goto end;
  224. }
  225. }
  226. if (in == NULL) {
  227. assert(passwds != NULL);
  228. assert(*passwds != NULL);
  229. do { /* loop over list of passwords */
  230. passwd = *passwds++;
  231. if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
  232. quiet, table, reverse, pw_maxlen, mode))
  233. goto end;
  234. } while (*passwds != NULL);
  235. } else {
  236. /* in != NULL */
  237. int done;
  238. assert(passwd != NULL);
  239. do {
  240. int r = BIO_gets(in, passwd, pw_maxlen + 1);
  241. if (r > 0) {
  242. char *c = (strchr(passwd, '\n'));
  243. if (c != NULL) {
  244. *c = 0; /* truncate at newline */
  245. } else {
  246. /* ignore rest of line */
  247. char trash[BUFSIZ];
  248. do
  249. r = BIO_gets(in, trash, sizeof(trash));
  250. while ((r > 0) && (!strchr(trash, '\n')));
  251. }
  252. if (!do_passwd
  253. (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
  254. table, reverse, pw_maxlen, mode))
  255. goto end;
  256. }
  257. done = (r <= 0);
  258. } while (!done);
  259. }
  260. ret = 0;
  261. end:
  262. #if 0
  263. ERR_print_errors(bio_err);
  264. #endif
  265. OPENSSL_free(salt_malloc);
  266. OPENSSL_free(passwd_malloc);
  267. BIO_free(in);
  268. return ret;
  269. }
  270. /*
  271. * MD5-based password algorithm (should probably be available as a library
  272. * function; then the static buffer would not be acceptable). For magic
  273. * string "1", this should be compatible to the MD5-based BSD password
  274. * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
  275. * Apache password algorithm. (Apparently, the Apache password algorithm is
  276. * identical except that the 'magic' string was changed -- the laziest
  277. * application of the NIH principle I've ever encountered.)
  278. */
  279. static char *md5crypt(const char *passwd, const char *magic, const char *salt)
  280. {
  281. /* "$apr1$..salt..$.......md5hash..........\0" */
  282. static char out_buf[6 + 9 + 24 + 2];
  283. unsigned char buf[MD5_DIGEST_LENGTH];
  284. char ascii_magic[5]; /* "apr1" plus '\0' */
  285. char ascii_salt[9]; /* Max 8 chars plus '\0' */
  286. char *ascii_passwd = NULL;
  287. char *salt_out;
  288. int n;
  289. unsigned int i;
  290. EVP_MD_CTX *md = NULL, *md2 = NULL;
  291. size_t passwd_len, salt_len, magic_len;
  292. passwd_len = strlen(passwd);
  293. out_buf[0] = 0;
  294. magic_len = strlen(magic);
  295. OPENSSL_strlcpy(ascii_magic, magic, sizeof(ascii_magic));
  296. #ifdef CHARSET_EBCDIC
  297. if ((magic[0] & 0x80) != 0) /* High bit is 1 in EBCDIC alnums */
  298. ebcdic2ascii(ascii_magic, ascii_magic, magic_len);
  299. #endif
  300. /* The salt gets truncated to 8 chars */
  301. OPENSSL_strlcpy(ascii_salt, salt, sizeof(ascii_salt));
  302. salt_len = strlen(ascii_salt);
  303. #ifdef CHARSET_EBCDIC
  304. ebcdic2ascii(ascii_salt, ascii_salt, salt_len);
  305. #endif
  306. #ifdef CHARSET_EBCDIC
  307. ascii_passwd = OPENSSL_strdup(passwd);
  308. if (ascii_passwd == NULL)
  309. return NULL;
  310. ebcdic2ascii(ascii_passwd, ascii_passwd, passwd_len);
  311. passwd = ascii_passwd;
  312. #endif
  313. if (magic_len > 0) {
  314. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  315. if (magic_len > 4) /* assert it's "1" or "apr1" */
  316. goto err;
  317. OPENSSL_strlcat(out_buf, ascii_magic, sizeof(out_buf));
  318. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  319. }
  320. OPENSSL_strlcat(out_buf, ascii_salt, sizeof(out_buf));
  321. if (strlen(out_buf) > 6 + 8) /* assert "$apr1$..salt.." */
  322. goto err;
  323. salt_out = out_buf;
  324. if (magic_len > 0)
  325. salt_out += 2 + magic_len;
  326. if (salt_len > 8)
  327. goto err;
  328. md = EVP_MD_CTX_new();
  329. if (md == NULL
  330. || !EVP_DigestInit_ex(md, EVP_md5(), NULL)
  331. || !EVP_DigestUpdate(md, passwd, passwd_len))
  332. goto err;
  333. if (magic_len > 0)
  334. if (!EVP_DigestUpdate(md, ascii_dollar, 1)
  335. || !EVP_DigestUpdate(md, ascii_magic, magic_len)
  336. || !EVP_DigestUpdate(md, ascii_dollar, 1))
  337. goto err;
  338. if (!EVP_DigestUpdate(md, ascii_salt, salt_len))
  339. goto err;
  340. md2 = EVP_MD_CTX_new();
  341. if (md2 == NULL
  342. || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
  343. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  344. || !EVP_DigestUpdate(md2, ascii_salt, salt_len)
  345. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  346. || !EVP_DigestFinal_ex(md2, buf, NULL))
  347. goto err;
  348. for (i = passwd_len; i > sizeof(buf); i -= sizeof(buf)) {
  349. if (!EVP_DigestUpdate(md, buf, sizeof(buf)))
  350. goto err;
  351. }
  352. if (!EVP_DigestUpdate(md, buf, i))
  353. goto err;
  354. n = passwd_len;
  355. while (n) {
  356. if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
  357. goto err;
  358. n >>= 1;
  359. }
  360. if (!EVP_DigestFinal_ex(md, buf, NULL))
  361. return NULL;
  362. for (i = 0; i < 1000; i++) {
  363. if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
  364. goto err;
  365. if (!EVP_DigestUpdate(md2,
  366. (i & 1) ? (unsigned const char *)passwd : buf,
  367. (i & 1) ? passwd_len : sizeof(buf)))
  368. goto err;
  369. if (i % 3) {
  370. if (!EVP_DigestUpdate(md2, ascii_salt, salt_len))
  371. goto err;
  372. }
  373. if (i % 7) {
  374. if (!EVP_DigestUpdate(md2, passwd, passwd_len))
  375. goto err;
  376. }
  377. if (!EVP_DigestUpdate(md2,
  378. (i & 1) ? buf : (unsigned const char *)passwd,
  379. (i & 1) ? sizeof(buf) : passwd_len))
  380. goto err;
  381. if (!EVP_DigestFinal_ex(md2, buf, NULL))
  382. goto err;
  383. }
  384. EVP_MD_CTX_free(md2);
  385. EVP_MD_CTX_free(md);
  386. md2 = NULL;
  387. md = NULL;
  388. {
  389. /* transform buf into output string */
  390. unsigned char buf_perm[sizeof(buf)];
  391. int dest, source;
  392. char *output;
  393. /* silly output permutation */
  394. for (dest = 0, source = 0; dest < 14;
  395. dest++, source = (source + 6) % 17)
  396. buf_perm[dest] = buf[source];
  397. buf_perm[14] = buf[5];
  398. buf_perm[15] = buf[11];
  399. # ifndef PEDANTIC /* Unfortunately, this generates a "no
  400. * effect" warning */
  401. assert(16 == sizeof(buf_perm));
  402. # endif
  403. output = salt_out + salt_len;
  404. assert(output == out_buf + strlen(out_buf));
  405. *output++ = ascii_dollar[0];
  406. for (i = 0; i < 15; i += 3) {
  407. *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
  408. *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
  409. (buf_perm[i + 2] >> 6)];
  410. *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
  411. (buf_perm[i + 1] >> 4)];
  412. *output++ = cov_2char[buf_perm[i] >> 2];
  413. }
  414. assert(i == 15);
  415. *output++ = cov_2char[buf_perm[i] & 0x3f];
  416. *output++ = cov_2char[buf_perm[i] >> 6];
  417. *output = 0;
  418. assert(strlen(out_buf) < sizeof(out_buf));
  419. #ifdef CHARSET_EBCDIC
  420. ascii2ebcdic(out_buf, out_buf, strlen(out_buf));
  421. #endif
  422. }
  423. return out_buf;
  424. err:
  425. OPENSSL_free(ascii_passwd);
  426. EVP_MD_CTX_free(md2);
  427. EVP_MD_CTX_free(md);
  428. return NULL;
  429. }
  430. /*
  431. * SHA based password algorithm, describe by Ulrich Drepper here:
  432. * https://www.akkadia.org/drepper/SHA-crypt.txt
  433. * (note that it's in the public domain)
  434. */
  435. static char *shacrypt(const char *passwd, const char *magic, const char *salt)
  436. {
  437. /* Prefix for optional rounds specification. */
  438. static const char rounds_prefix[] = "rounds=";
  439. /* Maximum salt string length. */
  440. # define SALT_LEN_MAX 16
  441. /* Default number of rounds if not explicitly specified. */
  442. # define ROUNDS_DEFAULT 5000
  443. /* Minimum number of rounds. */
  444. # define ROUNDS_MIN 1000
  445. /* Maximum number of rounds. */
  446. # define ROUNDS_MAX 999999999
  447. /* "$6$rounds=<N>$......salt......$...shahash(up to 86 chars)...\0" */
  448. static char out_buf[3 + 17 + 17 + 86 + 1];
  449. unsigned char buf[SHA512_DIGEST_LENGTH];
  450. unsigned char temp_buf[SHA512_DIGEST_LENGTH];
  451. size_t buf_size = 0;
  452. char ascii_magic[2];
  453. char ascii_salt[17]; /* Max 16 chars plus '\0' */
  454. char *ascii_passwd = NULL;
  455. size_t n;
  456. EVP_MD_CTX *md = NULL, *md2 = NULL;
  457. const EVP_MD *sha = NULL;
  458. size_t passwd_len, salt_len, magic_len;
  459. unsigned int rounds = 5000; /* Default */
  460. char rounds_custom = 0;
  461. char *p_bytes = NULL;
  462. char *s_bytes = NULL;
  463. char *cp = NULL;
  464. passwd_len = strlen(passwd);
  465. magic_len = strlen(magic);
  466. /* assert it's "5" or "6" */
  467. if (magic_len != 1)
  468. return NULL;
  469. switch (magic[0]) {
  470. case '5':
  471. sha = EVP_sha256();
  472. buf_size = 32;
  473. break;
  474. case '6':
  475. sha = EVP_sha512();
  476. buf_size = 64;
  477. break;
  478. default:
  479. return NULL;
  480. }
  481. if (strncmp(salt, rounds_prefix, sizeof(rounds_prefix) - 1) == 0) {
  482. const char *num = salt + sizeof(rounds_prefix) - 1;
  483. char *endp;
  484. unsigned long int srounds = strtoul (num, &endp, 10);
  485. if (*endp == '$') {
  486. salt = endp + 1;
  487. if (srounds > ROUNDS_MAX)
  488. rounds = ROUNDS_MAX;
  489. else if (srounds < ROUNDS_MIN)
  490. rounds = ROUNDS_MIN;
  491. else
  492. rounds = (unsigned int)srounds;
  493. rounds_custom = 1;
  494. } else {
  495. return NULL;
  496. }
  497. }
  498. OPENSSL_strlcpy(ascii_magic, magic, sizeof(ascii_magic));
  499. #ifdef CHARSET_EBCDIC
  500. if ((magic[0] & 0x80) != 0) /* High bit is 1 in EBCDIC alnums */
  501. ebcdic2ascii(ascii_magic, ascii_magic, magic_len);
  502. #endif
  503. /* The salt gets truncated to 16 chars */
  504. OPENSSL_strlcpy(ascii_salt, salt, sizeof(ascii_salt));
  505. salt_len = strlen(ascii_salt);
  506. #ifdef CHARSET_EBCDIC
  507. ebcdic2ascii(ascii_salt, ascii_salt, salt_len);
  508. #endif
  509. #ifdef CHARSET_EBCDIC
  510. ascii_passwd = OPENSSL_strdup(passwd);
  511. if (ascii_passwd == NULL)
  512. return NULL;
  513. ebcdic2ascii(ascii_passwd, ascii_passwd, passwd_len);
  514. passwd = ascii_passwd;
  515. #endif
  516. out_buf[0] = 0;
  517. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  518. OPENSSL_strlcat(out_buf, ascii_magic, sizeof(out_buf));
  519. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  520. if (rounds_custom) {
  521. char tmp_buf[80]; /* "rounds=999999999" */
  522. sprintf(tmp_buf, "rounds=%u", rounds);
  523. #ifdef CHARSET_EBCDIC
  524. /* In case we're really on a ASCII based platform and just pretend */
  525. if (tmp_buf[0] != 0x72) /* ASCII 'r' */
  526. ebcdic2ascii(tmp_buf, tmp_buf, strlen(tmp_buf));
  527. #endif
  528. OPENSSL_strlcat(out_buf, tmp_buf, sizeof(out_buf));
  529. OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
  530. }
  531. OPENSSL_strlcat(out_buf, ascii_salt, sizeof(out_buf));
  532. /* assert "$5$rounds=999999999$......salt......" */
  533. if (strlen(out_buf) > 3 + 17 * rounds_custom + salt_len )
  534. goto err;
  535. md = EVP_MD_CTX_new();
  536. if (md == NULL
  537. || !EVP_DigestInit_ex(md, sha, NULL)
  538. || !EVP_DigestUpdate(md, passwd, passwd_len)
  539. || !EVP_DigestUpdate(md, ascii_salt, salt_len))
  540. goto err;
  541. md2 = EVP_MD_CTX_new();
  542. if (md2 == NULL
  543. || !EVP_DigestInit_ex(md2, sha, NULL)
  544. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  545. || !EVP_DigestUpdate(md2, ascii_salt, salt_len)
  546. || !EVP_DigestUpdate(md2, passwd, passwd_len)
  547. || !EVP_DigestFinal_ex(md2, buf, NULL))
  548. goto err;
  549. for (n = passwd_len; n > buf_size; n -= buf_size) {
  550. if (!EVP_DigestUpdate(md, buf, buf_size))
  551. goto err;
  552. }
  553. if (!EVP_DigestUpdate(md, buf, n))
  554. goto err;
  555. n = passwd_len;
  556. while (n) {
  557. if (!EVP_DigestUpdate(md,
  558. (n & 1) ? buf : (unsigned const char *)passwd,
  559. (n & 1) ? buf_size : passwd_len))
  560. goto err;
  561. n >>= 1;
  562. }
  563. if (!EVP_DigestFinal_ex(md, buf, NULL))
  564. return NULL;
  565. /* P sequence */
  566. if (!EVP_DigestInit_ex(md2, sha, NULL))
  567. goto err;
  568. for (n = passwd_len; n > 0; n--)
  569. if (!EVP_DigestUpdate(md2, passwd, passwd_len))
  570. goto err;
  571. if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
  572. return NULL;
  573. if ((p_bytes = OPENSSL_zalloc(passwd_len)) == NULL)
  574. goto err;
  575. for (cp = p_bytes, n = passwd_len; n > buf_size; n -= buf_size, cp += buf_size)
  576. memcpy(cp, temp_buf, buf_size);
  577. memcpy(cp, temp_buf, n);
  578. /* S sequence */
  579. if (!EVP_DigestInit_ex(md2, sha, NULL))
  580. goto err;
  581. for (n = 16 + buf[0]; n > 0; n--)
  582. if (!EVP_DigestUpdate(md2, ascii_salt, salt_len))
  583. goto err;
  584. if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
  585. return NULL;
  586. if ((s_bytes = OPENSSL_zalloc(salt_len)) == NULL)
  587. goto err;
  588. for (cp = s_bytes, n = salt_len; n > buf_size; n -= buf_size, cp += buf_size)
  589. memcpy(cp, temp_buf, buf_size);
  590. memcpy(cp, temp_buf, n);
  591. for (n = 0; n < rounds; n++) {
  592. if (!EVP_DigestInit_ex(md2, sha, NULL))
  593. goto err;
  594. if (!EVP_DigestUpdate(md2,
  595. (n & 1) ? (unsigned const char *)p_bytes : buf,
  596. (n & 1) ? passwd_len : buf_size))
  597. goto err;
  598. if (n % 3) {
  599. if (!EVP_DigestUpdate(md2, s_bytes, salt_len))
  600. goto err;
  601. }
  602. if (n % 7) {
  603. if (!EVP_DigestUpdate(md2, p_bytes, passwd_len))
  604. goto err;
  605. }
  606. if (!EVP_DigestUpdate(md2,
  607. (n & 1) ? buf : (unsigned const char *)p_bytes,
  608. (n & 1) ? buf_size : passwd_len))
  609. goto err;
  610. if (!EVP_DigestFinal_ex(md2, buf, NULL))
  611. goto err;
  612. }
  613. EVP_MD_CTX_free(md2);
  614. EVP_MD_CTX_free(md);
  615. md2 = NULL;
  616. md = NULL;
  617. OPENSSL_free(p_bytes);
  618. OPENSSL_free(s_bytes);
  619. p_bytes = NULL;
  620. s_bytes = NULL;
  621. cp = out_buf + strlen(out_buf);
  622. *cp++ = ascii_dollar[0];
  623. # define b64_from_24bit(B2, B1, B0, N) \
  624. do { \
  625. unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
  626. int i = (N); \
  627. while (i-- > 0) \
  628. { \
  629. *cp++ = cov_2char[w & 0x3f]; \
  630. w >>= 6; \
  631. } \
  632. } while (0)
  633. switch (magic[0]) {
  634. case '5':
  635. b64_from_24bit (buf[0], buf[10], buf[20], 4);
  636. b64_from_24bit (buf[21], buf[1], buf[11], 4);
  637. b64_from_24bit (buf[12], buf[22], buf[2], 4);
  638. b64_from_24bit (buf[3], buf[13], buf[23], 4);
  639. b64_from_24bit (buf[24], buf[4], buf[14], 4);
  640. b64_from_24bit (buf[15], buf[25], buf[5], 4);
  641. b64_from_24bit (buf[6], buf[16], buf[26], 4);
  642. b64_from_24bit (buf[27], buf[7], buf[17], 4);
  643. b64_from_24bit (buf[18], buf[28], buf[8], 4);
  644. b64_from_24bit (buf[9], buf[19], buf[29], 4);
  645. b64_from_24bit (0, buf[31], buf[30], 3);
  646. break;
  647. case '6':
  648. b64_from_24bit (buf[0], buf[21], buf[42], 4);
  649. b64_from_24bit (buf[22], buf[43], buf[1], 4);
  650. b64_from_24bit (buf[44], buf[2], buf[23], 4);
  651. b64_from_24bit (buf[3], buf[24], buf[45], 4);
  652. b64_from_24bit (buf[25], buf[46], buf[4], 4);
  653. b64_from_24bit (buf[47], buf[5], buf[26], 4);
  654. b64_from_24bit (buf[6], buf[27], buf[48], 4);
  655. b64_from_24bit (buf[28], buf[49], buf[7], 4);
  656. b64_from_24bit (buf[50], buf[8], buf[29], 4);
  657. b64_from_24bit (buf[9], buf[30], buf[51], 4);
  658. b64_from_24bit (buf[31], buf[52], buf[10], 4);
  659. b64_from_24bit (buf[53], buf[11], buf[32], 4);
  660. b64_from_24bit (buf[12], buf[33], buf[54], 4);
  661. b64_from_24bit (buf[34], buf[55], buf[13], 4);
  662. b64_from_24bit (buf[56], buf[14], buf[35], 4);
  663. b64_from_24bit (buf[15], buf[36], buf[57], 4);
  664. b64_from_24bit (buf[37], buf[58], buf[16], 4);
  665. b64_from_24bit (buf[59], buf[17], buf[38], 4);
  666. b64_from_24bit (buf[18], buf[39], buf[60], 4);
  667. b64_from_24bit (buf[40], buf[61], buf[19], 4);
  668. b64_from_24bit (buf[62], buf[20], buf[41], 4);
  669. b64_from_24bit (0, 0, buf[63], 2);
  670. break;
  671. default:
  672. goto err;
  673. }
  674. *cp = '\0';
  675. #ifdef CHARSET_EBCDIC
  676. ascii2ebcdic(out_buf, out_buf, strlen(out_buf));
  677. #endif
  678. return out_buf;
  679. err:
  680. EVP_MD_CTX_free(md2);
  681. EVP_MD_CTX_free(md);
  682. OPENSSL_free(p_bytes);
  683. OPENSSL_free(s_bytes);
  684. OPENSSL_free(ascii_passwd);
  685. return NULL;
  686. }
  687. static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
  688. char *passwd, BIO *out, int quiet, int table,
  689. int reverse, size_t pw_maxlen, passwd_modes mode)
  690. {
  691. char *hash = NULL;
  692. assert(salt_p != NULL);
  693. assert(salt_malloc_p != NULL);
  694. /* first make sure we have a salt */
  695. if (!passed_salt) {
  696. size_t saltlen = 0;
  697. size_t i;
  698. if (mode == passwd_md5 || mode == passwd_apr1 || mode == passwd_aixmd5)
  699. saltlen = 8;
  700. if (mode == passwd_sha256 || mode == passwd_sha512)
  701. saltlen = 16;
  702. assert(saltlen != 0);
  703. if (*salt_malloc_p == NULL)
  704. *salt_p = *salt_malloc_p = app_malloc(saltlen + 1, "salt buffer");
  705. if (RAND_bytes((unsigned char *)*salt_p, saltlen) <= 0)
  706. goto end;
  707. for (i = 0; i < saltlen; i++)
  708. (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
  709. (*salt_p)[i] = 0;
  710. # ifdef CHARSET_EBCDIC
  711. /* The password encryption function will convert back to ASCII */
  712. ascii2ebcdic(*salt_p, *salt_p, saltlen);
  713. # endif
  714. }
  715. assert(*salt_p != NULL);
  716. /* truncate password if necessary */
  717. if ((strlen(passwd) > pw_maxlen)) {
  718. if (!quiet)
  719. /*
  720. * XXX: really we should know how to print a size_t, not cast it
  721. */
  722. BIO_printf(bio_err,
  723. "Warning: truncating password to %u characters\n",
  724. (unsigned)pw_maxlen);
  725. passwd[pw_maxlen] = 0;
  726. }
  727. assert(strlen(passwd) <= pw_maxlen);
  728. /* now compute password hash */
  729. if (mode == passwd_md5 || mode == passwd_apr1)
  730. hash = md5crypt(passwd, (mode == passwd_md5 ? "1" : "apr1"), *salt_p);
  731. if (mode == passwd_aixmd5)
  732. hash = md5crypt(passwd, "", *salt_p);
  733. if (mode == passwd_sha256 || mode == passwd_sha512)
  734. hash = shacrypt(passwd, (mode == passwd_sha256 ? "5" : "6"), *salt_p);
  735. assert(hash != NULL);
  736. if (table && !reverse)
  737. BIO_printf(out, "%s\t%s\n", passwd, hash);
  738. else if (table && reverse)
  739. BIO_printf(out, "%s\t%s\n", hash, passwd);
  740. else
  741. BIO_printf(out, "%s\n", hash);
  742. return 1;
  743. end:
  744. return 0;
  745. }