2
0

passwd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* apps/passwd.c */
  2. #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
  3. # define NO_MD5CRYPT_1
  4. #endif
  5. #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
  6. # include <assert.h>
  7. # include <string.h>
  8. # include "apps.h"
  9. # include <openssl/bio.h>
  10. # include <openssl/err.h>
  11. # include <openssl/evp.h>
  12. # include <openssl/rand.h>
  13. # ifndef OPENSSL_NO_DES
  14. # include <openssl/des.h>
  15. # endif
  16. # ifndef NO_MD5CRYPT_1
  17. # include <openssl/md5.h>
  18. # endif
  19. # undef PROG
  20. # define PROG passwd_main
  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 int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
  33. char *passwd, BIO *out, int quiet, int table,
  34. int reverse, size_t pw_maxlen, int usecrypt, int use1,
  35. int useapr1);
  36. /*-
  37. * -crypt - standard Unix password algorithm (default)
  38. * -1 - MD5-based password algorithm
  39. * -apr1 - MD5-based password algorithm, Apache variant
  40. * -salt string - salt
  41. * -in file - read passwords from file
  42. * -stdin - read passwords from stdin
  43. * -noverify - never verify when reading password from terminal
  44. * -quiet - no warnings
  45. * -table - format output as table
  46. * -reverse - switch table columns
  47. */
  48. int MAIN(int, char **);
  49. int MAIN(int argc, char **argv)
  50. {
  51. int ret = 1;
  52. char *infile = NULL;
  53. int in_stdin = 0;
  54. int in_noverify = 0;
  55. char *salt = NULL, *passwd = NULL, **passwds = NULL;
  56. char *salt_malloc = NULL, *passwd_malloc = NULL;
  57. size_t passwd_malloc_size = 0;
  58. int pw_source_defined = 0;
  59. BIO *in = NULL, *out = NULL;
  60. int i, badopt, opt_done;
  61. int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
  62. int usecrypt = 0, use1 = 0, useapr1 = 0;
  63. size_t pw_maxlen = 0;
  64. apps_startup();
  65. if (bio_err == NULL)
  66. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  67. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  68. if (!load_config(bio_err, NULL))
  69. goto err;
  70. out = BIO_new(BIO_s_file());
  71. if (out == NULL)
  72. goto err;
  73. BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
  74. # ifdef OPENSSL_SYS_VMS
  75. {
  76. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  77. out = BIO_push(tmpbio, out);
  78. }
  79. # endif
  80. badopt = 0, opt_done = 0;
  81. i = 0;
  82. while (!badopt && !opt_done && argv[++i] != NULL) {
  83. if (strcmp(argv[i], "-crypt") == 0)
  84. usecrypt = 1;
  85. else if (strcmp(argv[i], "-1") == 0)
  86. use1 = 1;
  87. else if (strcmp(argv[i], "-apr1") == 0)
  88. useapr1 = 1;
  89. else if (strcmp(argv[i], "-salt") == 0) {
  90. if ((argv[i + 1] != NULL) && (salt == NULL)) {
  91. passed_salt = 1;
  92. salt = argv[++i];
  93. } else
  94. badopt = 1;
  95. } else if (strcmp(argv[i], "-in") == 0) {
  96. if ((argv[i + 1] != NULL) && !pw_source_defined) {
  97. pw_source_defined = 1;
  98. infile = argv[++i];
  99. } else
  100. badopt = 1;
  101. } else if (strcmp(argv[i], "-stdin") == 0) {
  102. if (!pw_source_defined) {
  103. pw_source_defined = 1;
  104. in_stdin = 1;
  105. } else
  106. badopt = 1;
  107. } else if (strcmp(argv[i], "-noverify") == 0)
  108. in_noverify = 1;
  109. else if (strcmp(argv[i], "-quiet") == 0)
  110. quiet = 1;
  111. else if (strcmp(argv[i], "-table") == 0)
  112. table = 1;
  113. else if (strcmp(argv[i], "-reverse") == 0)
  114. reverse = 1;
  115. else if (argv[i][0] == '-')
  116. badopt = 1;
  117. else if (!pw_source_defined)
  118. /* non-option arguments, use as passwords */
  119. {
  120. pw_source_defined = 1;
  121. passwds = &argv[i];
  122. opt_done = 1;
  123. } else
  124. badopt = 1;
  125. }
  126. if (!usecrypt && !use1 && !useapr1) /* use default */
  127. usecrypt = 1;
  128. if (usecrypt + use1 + useapr1 > 1) /* conflict */
  129. badopt = 1;
  130. /* reject unsupported algorithms */
  131. # ifdef OPENSSL_NO_DES
  132. if (usecrypt)
  133. badopt = 1;
  134. # endif
  135. # ifdef NO_MD5CRYPT_1
  136. if (use1 || useapr1)
  137. badopt = 1;
  138. # endif
  139. if (badopt) {
  140. BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
  141. BIO_printf(bio_err, "where options are\n");
  142. # ifndef OPENSSL_NO_DES
  143. BIO_printf(bio_err,
  144. "-crypt standard Unix password algorithm (default)\n");
  145. # endif
  146. # ifndef NO_MD5CRYPT_1
  147. BIO_printf(bio_err,
  148. "-1 MD5-based password algorithm\n");
  149. BIO_printf(bio_err,
  150. "-apr1 MD5-based password algorithm, Apache variant\n");
  151. # endif
  152. BIO_printf(bio_err, "-salt string use provided salt\n");
  153. BIO_printf(bio_err, "-in file read passwords from file\n");
  154. BIO_printf(bio_err, "-stdin read passwords from stdin\n");
  155. BIO_printf(bio_err,
  156. "-noverify never verify when reading password from terminal\n");
  157. BIO_printf(bio_err, "-quiet no warnings\n");
  158. BIO_printf(bio_err, "-table format output as table\n");
  159. BIO_printf(bio_err, "-reverse switch table columns\n");
  160. goto err;
  161. }
  162. if ((infile != NULL) || in_stdin) {
  163. in = BIO_new(BIO_s_file());
  164. if (in == NULL)
  165. goto err;
  166. if (infile != NULL) {
  167. assert(in_stdin == 0);
  168. if (BIO_read_filename(in, infile) <= 0)
  169. goto err;
  170. } else {
  171. assert(in_stdin);
  172. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  173. }
  174. }
  175. if (usecrypt)
  176. pw_maxlen = 8;
  177. else if (use1 || useapr1)
  178. pw_maxlen = 256; /* arbitrary limit, should be enough for most
  179. * passwords */
  180. if (passwds == NULL) {
  181. /* no passwords on the command line */
  182. passwd_malloc_size = pw_maxlen + 2;
  183. /*
  184. * longer than necessary so that we can warn about truncation
  185. */
  186. passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
  187. if (passwd_malloc == NULL)
  188. goto err;
  189. }
  190. if ((in == NULL) && (passwds == NULL)) {
  191. /* build a null-terminated list */
  192. static char *passwds_static[2] = { NULL, NULL };
  193. passwds = passwds_static;
  194. if (in == NULL)
  195. if (EVP_read_pw_string
  196. (passwd_malloc, passwd_malloc_size, "Password: ",
  197. !(passed_salt || in_noverify)) != 0)
  198. goto err;
  199. passwds[0] = passwd_malloc;
  200. }
  201. if (in == NULL) {
  202. assert(passwds != NULL);
  203. assert(*passwds != NULL);
  204. do { /* loop over list of passwords */
  205. passwd = *passwds++;
  206. if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
  207. quiet, table, reverse, pw_maxlen, usecrypt, use1,
  208. useapr1))
  209. goto err;
  210. }
  211. while (*passwds != NULL);
  212. } else
  213. /* in != NULL */
  214. {
  215. int done;
  216. assert(passwd != NULL);
  217. do {
  218. int r = BIO_gets(in, passwd, pw_maxlen + 1);
  219. if (r > 0) {
  220. char *c = (strchr(passwd, '\n'));
  221. if (c != NULL)
  222. *c = 0; /* truncate at newline */
  223. else {
  224. /* ignore rest of line */
  225. char trash[BUFSIZ];
  226. do
  227. r = BIO_gets(in, trash, sizeof trash);
  228. while ((r > 0) && (!strchr(trash, '\n')));
  229. }
  230. if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
  231. quiet, table, reverse, pw_maxlen, usecrypt,
  232. use1, useapr1))
  233. goto err;
  234. }
  235. done = (r <= 0);
  236. }
  237. while (!done);
  238. }
  239. ret = 0;
  240. err:
  241. ERR_print_errors(bio_err);
  242. if (salt_malloc)
  243. OPENSSL_free(salt_malloc);
  244. if (passwd_malloc)
  245. OPENSSL_free(passwd_malloc);
  246. if (in)
  247. BIO_free(in);
  248. if (out)
  249. BIO_free_all(out);
  250. apps_shutdown();
  251. OPENSSL_EXIT(ret);
  252. }
  253. # ifndef NO_MD5CRYPT_1
  254. /*
  255. * MD5-based password algorithm (should probably be available as a library
  256. * function; then the static buffer would not be acceptable). For magic
  257. * string "1", this should be compatible to the MD5-based BSD password
  258. * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
  259. * Apache password algorithm. (Apparently, the Apache password algorithm is
  260. * identical except that the 'magic' string was changed -- the laziest
  261. * application of the NIH principle I've ever encountered.)
  262. */
  263. static char *md5crypt(const char *passwd, const char *magic, const char *salt)
  264. {
  265. /* "$apr1$..salt..$.......md5hash..........\0" */
  266. static char out_buf[6 + 9 + 24 + 2];
  267. unsigned char buf[MD5_DIGEST_LENGTH];
  268. char *salt_out;
  269. int n;
  270. unsigned int i;
  271. EVP_MD_CTX md, md2;
  272. size_t passwd_len, salt_len;
  273. passwd_len = strlen(passwd);
  274. out_buf[0] = '$';
  275. out_buf[1] = 0;
  276. assert(strlen(magic) <= 4); /* "1" or "apr1" */
  277. strncat(out_buf, magic, 4);
  278. strncat(out_buf, "$", 1);
  279. strncat(out_buf, salt, 8);
  280. assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
  281. salt_out = out_buf + 2 + strlen(magic);
  282. salt_len = strlen(salt_out);
  283. assert(salt_len <= 8);
  284. EVP_MD_CTX_init(&md);
  285. EVP_DigestInit_ex(&md, EVP_md5(), NULL);
  286. EVP_DigestUpdate(&md, passwd, passwd_len);
  287. EVP_DigestUpdate(&md, "$", 1);
  288. EVP_DigestUpdate(&md, magic, strlen(magic));
  289. EVP_DigestUpdate(&md, "$", 1);
  290. EVP_DigestUpdate(&md, salt_out, salt_len);
  291. EVP_MD_CTX_init(&md2);
  292. EVP_DigestInit_ex(&md2, EVP_md5(), NULL);
  293. EVP_DigestUpdate(&md2, passwd, passwd_len);
  294. EVP_DigestUpdate(&md2, salt_out, salt_len);
  295. EVP_DigestUpdate(&md2, passwd, passwd_len);
  296. EVP_DigestFinal_ex(&md2, buf, NULL);
  297. for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
  298. EVP_DigestUpdate(&md, buf, sizeof buf);
  299. EVP_DigestUpdate(&md, buf, i);
  300. n = passwd_len;
  301. while (n) {
  302. EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
  303. n >>= 1;
  304. }
  305. EVP_DigestFinal_ex(&md, buf, NULL);
  306. for (i = 0; i < 1000; i++) {
  307. EVP_DigestInit_ex(&md2, EVP_md5(), NULL);
  308. EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *)passwd : buf,
  309. (i & 1) ? passwd_len : sizeof buf);
  310. if (i % 3)
  311. EVP_DigestUpdate(&md2, salt_out, salt_len);
  312. if (i % 7)
  313. EVP_DigestUpdate(&md2, passwd, passwd_len);
  314. EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *)passwd,
  315. (i & 1) ? sizeof buf : passwd_len);
  316. EVP_DigestFinal_ex(&md2, buf, NULL);
  317. }
  318. EVP_MD_CTX_cleanup(&md2);
  319. {
  320. /* transform buf into output string */
  321. unsigned char buf_perm[sizeof buf];
  322. int dest, source;
  323. char *output;
  324. /* silly output permutation */
  325. for (dest = 0, source = 0; dest < 14;
  326. dest++, source = (source + 6) % 17)
  327. buf_perm[dest] = buf[source];
  328. buf_perm[14] = buf[5];
  329. buf_perm[15] = buf[11];
  330. # ifndef PEDANTIC /* Unfortunately, this generates a "no
  331. * effect" warning */
  332. assert(16 == sizeof buf_perm);
  333. # endif
  334. output = salt_out + salt_len;
  335. assert(output == out_buf + strlen(out_buf));
  336. *output++ = '$';
  337. for (i = 0; i < 15; i += 3) {
  338. *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
  339. *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
  340. (buf_perm[i + 2] >> 6)];
  341. *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
  342. (buf_perm[i + 1] >> 4)];
  343. *output++ = cov_2char[buf_perm[i] >> 2];
  344. }
  345. assert(i == 15);
  346. *output++ = cov_2char[buf_perm[i] & 0x3f];
  347. *output++ = cov_2char[buf_perm[i] >> 6];
  348. *output = 0;
  349. assert(strlen(out_buf) < sizeof(out_buf));
  350. }
  351. EVP_MD_CTX_cleanup(&md);
  352. return out_buf;
  353. }
  354. # endif
  355. static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
  356. char *passwd, BIO *out, int quiet, int table,
  357. int reverse, size_t pw_maxlen, int usecrypt, int use1,
  358. int useapr1)
  359. {
  360. char *hash = NULL;
  361. assert(salt_p != NULL);
  362. assert(salt_malloc_p != NULL);
  363. /* first make sure we have a salt */
  364. if (!passed_salt) {
  365. # ifndef OPENSSL_NO_DES
  366. if (usecrypt) {
  367. if (*salt_malloc_p == NULL) {
  368. *salt_p = *salt_malloc_p = OPENSSL_malloc(3);
  369. if (*salt_malloc_p == NULL)
  370. goto err;
  371. }
  372. if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
  373. goto err;
  374. (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
  375. (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
  376. (*salt_p)[2] = 0;
  377. # ifdef CHARSET_EBCDIC
  378. ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert back
  379. * to ASCII */
  380. # endif
  381. }
  382. # endif /* !OPENSSL_NO_DES */
  383. # ifndef NO_MD5CRYPT_1
  384. if (use1 || useapr1) {
  385. int i;
  386. if (*salt_malloc_p == NULL) {
  387. *salt_p = *salt_malloc_p = OPENSSL_malloc(9);
  388. if (*salt_malloc_p == NULL)
  389. goto err;
  390. }
  391. if (RAND_bytes((unsigned char *)*salt_p, 8) <= 0)
  392. goto err;
  393. for (i = 0; i < 8; i++)
  394. (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
  395. (*salt_p)[8] = 0;
  396. }
  397. # endif /* !NO_MD5CRYPT_1 */
  398. }
  399. assert(*salt_p != NULL);
  400. /* truncate password if necessary */
  401. if ((strlen(passwd) > pw_maxlen)) {
  402. if (!quiet)
  403. /*
  404. * XXX: really we should know how to print a size_t, not cast it
  405. */
  406. BIO_printf(bio_err,
  407. "Warning: truncating password to %u characters\n",
  408. (unsigned)pw_maxlen);
  409. passwd[pw_maxlen] = 0;
  410. }
  411. assert(strlen(passwd) <= pw_maxlen);
  412. /* now compute password hash */
  413. # ifndef OPENSSL_NO_DES
  414. if (usecrypt)
  415. hash = DES_crypt(passwd, *salt_p);
  416. # endif
  417. # ifndef NO_MD5CRYPT_1
  418. if (use1 || useapr1)
  419. hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
  420. # endif
  421. assert(hash != NULL);
  422. if (table && !reverse)
  423. BIO_printf(out, "%s\t%s\n", passwd, hash);
  424. else if (table && reverse)
  425. BIO_printf(out, "%s\t%s\n", hash, passwd);
  426. else
  427. BIO_printf(out, "%s\n", hash);
  428. return 1;
  429. err:
  430. return 0;
  431. }
  432. #else
  433. int MAIN(int argc, char **argv)
  434. {
  435. fputs("Program not available.\n", stderr)
  436. OPENSSL_EXIT(1);
  437. }
  438. #endif