passwd.c 26 KB

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