passwd.c 26 KB

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