2
0

passwd.c 13 KB

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