passwd.c 27 KB

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