]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/passwd.c
mark all block comments that need format preserving so that
[thirdparty/openssl.git] / apps / passwd.c
CommitLineData
bb325c7d
BM
1/* apps/passwd.c */
2
cf1b7d96 3#if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
1f4643a2 4# define NO_MD5CRYPT_1
3ebf0be1
BM
5#endif
6
cf1b7d96 7#if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
bb325c7d
BM
8
9#include <assert.h>
10#include <string.h>
11
12#include "apps.h"
13
14#include <openssl/bio.h>
15#include <openssl/err.h>
16#include <openssl/evp.h>
17#include <openssl/rand.h>
cf1b7d96 18#ifndef OPENSSL_NO_DES
125cc35b 19# include <openssl/des.h>
bb325c7d 20#endif
1f4643a2 21#ifndef NO_MD5CRYPT_1
dbad1690 22# include <openssl/md5.h>
e6e7b5f3
BM
23#endif
24
bb325c7d
BM
25
26#undef PROG
27#define PROG passwd_main
28
29
30static unsigned const char cov_2char[64]={
31 /* from crypto/des/fcrypt.c */
32 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
33 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
34 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
35 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
36 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
37 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
38 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
39 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
40};
41
e6e7b5f3
BM
42static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
43 char *passwd, BIO *out, int quiet, int table, int reverse,
1f4643a2 44 size_t pw_maxlen, int usecrypt, int use1, int useapr1);
e6e7b5f3 45
3e9a08ec
TH
46/*-
47 * -crypt - standard Unix password algorithm (default)
1f4643a2
BM
48 * -1 - MD5-based password algorithm
49 * -apr1 - MD5-based password algorithm, Apache variant
bb325c7d 50 * -salt string - salt
e6e7b5f3
BM
51 * -in file - read passwords from file
52 * -stdin - read passwords from stdin
db70a3fd 53 * -noverify - never verify when reading password from terminal
bb325c7d
BM
54 * -quiet - no warnings
55 * -table - format output as table
e6e7b5f3 56 * -reverse - switch table columns
bb325c7d
BM
57 */
58
07fb39c3
RE
59int MAIN(int, char **);
60
bb325c7d
BM
61int MAIN(int argc, char **argv)
62 {
63 int ret = 1;
e6e7b5f3
BM
64 char *infile = NULL;
65 int in_stdin = 0;
db70a3fd 66 int in_noverify = 0;
e6e7b5f3 67 char *salt = NULL, *passwd = NULL, **passwds = NULL;
bb325c7d 68 char *salt_malloc = NULL, *passwd_malloc = NULL;
7fc840cc 69 size_t passwd_malloc_size = 0;
e6e7b5f3
BM
70 int pw_source_defined = 0;
71 BIO *in = NULL, *out = NULL;
bb325c7d 72 int i, badopt, opt_done;
e6e7b5f3 73 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
1f4643a2 74 int usecrypt = 0, use1 = 0, useapr1 = 0;
bb325c7d
BM
75 size_t pw_maxlen = 0;
76
77 apps_startup();
78
79 if (bio_err == NULL)
80 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
81 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
3647bee2
DSH
82
83 if (!load_config(bio_err, NULL))
84 goto err;
bb325c7d
BM
85 out = BIO_new(BIO_s_file());
86 if (out == NULL)
87 goto err;
88 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
bc36ee62 89#ifdef OPENSSL_SYS_VMS
645749ef
RL
90 {
91 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
92 out = BIO_push(tmpbio, out);
93 }
94#endif
bb325c7d
BM
95
96 badopt = 0, opt_done = 0;
97 i = 0;
98 while (!badopt && !opt_done && argv[++i] != NULL)
99 {
100 if (strcmp(argv[i], "-crypt") == 0)
efb41629 101 usecrypt = 1;
1f4643a2
BM
102 else if (strcmp(argv[i], "-1") == 0)
103 use1 = 1;
e6e7b5f3
BM
104 else if (strcmp(argv[i], "-apr1") == 0)
105 useapr1 = 1;
bb325c7d
BM
106 else if (strcmp(argv[i], "-salt") == 0)
107 {
108 if ((argv[i+1] != NULL) && (salt == NULL))
109 {
110 passed_salt = 1;
111 salt = argv[++i];
112 }
e6e7b5f3
BM
113 else
114 badopt = 1;
115 }
116 else if (strcmp(argv[i], "-in") == 0)
117 {
118 if ((argv[i+1] != NULL) && !pw_source_defined)
119 {
120 pw_source_defined = 1;
121 infile = argv[++i];
122 }
123 else
124 badopt = 1;
125 }
126 else if (strcmp(argv[i], "-stdin") == 0)
127 {
128 if (!pw_source_defined)
129 {
130 pw_source_defined = 1;
131 in_stdin = 1;
132 }
133 else
134 badopt = 1;
bb325c7d 135 }
db70a3fd
BM
136 else if (strcmp(argv[i], "-noverify") == 0)
137 in_noverify = 1;
bb325c7d
BM
138 else if (strcmp(argv[i], "-quiet") == 0)
139 quiet = 1;
140 else if (strcmp(argv[i], "-table") == 0)
141 table = 1;
e6e7b5f3
BM
142 else if (strcmp(argv[i], "-reverse") == 0)
143 reverse = 1;
bb325c7d
BM
144 else if (argv[i][0] == '-')
145 badopt = 1;
e6e7b5f3
BM
146 else if (!pw_source_defined)
147 /* non-option arguments, use as passwords */
bb325c7d 148 {
e6e7b5f3 149 pw_source_defined = 1;
bb325c7d
BM
150 passwds = &argv[i];
151 opt_done = 1;
152 }
e6e7b5f3
BM
153 else
154 badopt = 1;
bb325c7d
BM
155 }
156
1f4643a2 157 if (!usecrypt && !use1 && !useapr1) /* use default */
efb41629 158 usecrypt = 1;
1f4643a2 159 if (usecrypt + use1 + useapr1 > 1) /* conflict */
bb325c7d
BM
160 badopt = 1;
161
e6e7b5f3 162 /* reject unsupported algorithms */
cf1b7d96 163#ifdef OPENSSL_NO_DES
e6e7b5f3
BM
164 if (usecrypt) badopt = 1;
165#endif
1f4643a2
BM
166#ifdef NO_MD5CRYPT_1
167 if (use1 || useapr1) badopt = 1;
e6e7b5f3
BM
168#endif
169
bb325c7d
BM
170 if (badopt)
171 {
172 BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
173 BIO_printf(bio_err, "where options are\n");
cf1b7d96 174#ifndef OPENSSL_NO_DES
bb325c7d 175 BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n");
e6e7b5f3 176#endif
1f4643a2
BM
177#ifndef NO_MD5CRYPT_1
178 BIO_printf(bio_err, "-1 MD5-based password algorithm\n");
179 BIO_printf(bio_err, "-apr1 MD5-based password algorithm, Apache variant\n");
e6e7b5f3 180#endif
bb325c7d 181 BIO_printf(bio_err, "-salt string use provided salt\n");
e6e7b5f3
BM
182 BIO_printf(bio_err, "-in file read passwords from file\n");
183 BIO_printf(bio_err, "-stdin read passwords from stdin\n");
db70a3fd 184 BIO_printf(bio_err, "-noverify never verify when reading password from terminal\n");
bb325c7d
BM
185 BIO_printf(bio_err, "-quiet no warnings\n");
186 BIO_printf(bio_err, "-table format output as table\n");
e6e7b5f3 187 BIO_printf(bio_err, "-reverse switch table columns\n");
bb325c7d
BM
188
189 goto err;
190 }
191
e6e7b5f3
BM
192 if ((infile != NULL) || in_stdin)
193 {
194 in = BIO_new(BIO_s_file());
195 if (in == NULL)
196 goto err;
197 if (infile != NULL)
198 {
199 assert(in_stdin == 0);
200 if (BIO_read_filename(in, infile) <= 0)
201 goto err;
202 }
203 else
204 {
205 assert(in_stdin);
206 BIO_set_fp(in, stdin, BIO_NOCLOSE);
207 }
208 }
209
efb41629 210 if (usecrypt)
bb325c7d 211 pw_maxlen = 8;
1f4643a2 212 else if (use1 || useapr1)
e6e7b5f3 213 pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */
bb325c7d
BM
214
215 if (passwds == NULL)
e6e7b5f3
BM
216 {
217 /* no passwords on the command line */
7fc840cc
BM
218
219 passwd_malloc_size = pw_maxlen + 2;
4adcfa05 220 /* longer than necessary so that we can warn about truncation */
26a3a48d 221 passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
e6e7b5f3
BM
222 if (passwd_malloc == NULL)
223 goto err;
224 }
225
226 if ((in == NULL) && (passwds == NULL))
bb325c7d
BM
227 {
228 /* build a null-terminated list */
229 static char *passwds_static[2] = {NULL, NULL};
e6e7b5f3 230
bb325c7d 231 passwds = passwds_static;
e6e7b5f3 232 if (in == NULL)
db70a3fd 233 if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", !(passed_salt || in_noverify)) != 0)
e6e7b5f3 234 goto err;
bb325c7d
BM
235 passwds[0] = passwd_malloc;
236 }
237
e6e7b5f3
BM
238 if (in == NULL)
239 {
240 assert(passwds != NULL);
241 assert(*passwds != NULL);
242
243 do /* loop over list of passwords */
244 {
245 passwd = *passwds++;
246 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
1f4643a2 247 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
e6e7b5f3
BM
248 goto err;
249 }
250 while (*passwds != NULL);
251 }
252 else
253 /* in != NULL */
bb325c7d 254 {
e6e7b5f3
BM
255 int done;
256
257 assert (passwd != NULL);
258 do
bb325c7d 259 {
e6e7b5f3
BM
260 int r = BIO_gets(in, passwd, pw_maxlen + 1);
261 if (r > 0)
bb325c7d 262 {
e6e7b5f3
BM
263 char *c = (strchr(passwd, '\n')) ;
264 if (c != NULL)
265 *c = 0; /* truncate at newline */
266 else
bb325c7d 267 {
e6e7b5f3
BM
268 /* ignore rest of line */
269 char trash[BUFSIZ];
270 do
271 r = BIO_gets(in, trash, sizeof trash);
272 while ((r > 0) && (!strchr(trash, '\n')));
bb325c7d 273 }
e6e7b5f3
BM
274
275 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
1f4643a2 276 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
bb325c7d 277 goto err;
bb325c7d 278 }
e6e7b5f3 279 done = (r <= 0);
bb325c7d 280 }
e6e7b5f3 281 while (!done);
bb325c7d 282 }
cad4b840 283 ret = 0;
e6e7b5f3 284
bb325c7d
BM
285err:
286 ERR_print_errors(bio_err);
287 if (salt_malloc)
26a3a48d 288 OPENSSL_free(salt_malloc);
bb325c7d 289 if (passwd_malloc)
26a3a48d 290 OPENSSL_free(passwd_malloc);
e6e7b5f3
BM
291 if (in)
292 BIO_free(in);
bb325c7d 293 if (out)
645749ef 294 BIO_free_all(out);
c04f8cf4 295 apps_shutdown();
1c3e4a36 296 OPENSSL_EXIT(ret);
bb325c7d 297 }
e6e7b5f3
BM
298
299
1f4643a2
BM
300#ifndef NO_MD5CRYPT_1
301/* MD5-based password algorithm (should probably be available as a library
302 * function; then the static buffer would not be acceptable).
303 * For magic string "1", this should be compatible to the MD5-based BSD
304 * password algorithm.
305 * For 'magic' string "apr1", this is compatible to the MD5-based Apache
306 * password algorithm.
307 * (Apparently, the Apache password algorithm is identical except that the
308 * 'magic' string was changed -- the laziest application of the NIH principle
309 * I've ever encountered.)
310 */
311static char *md5crypt(const char *passwd, const char *magic, const char *salt)
e6e7b5f3
BM
312 {
313 static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
314 unsigned char buf[MD5_DIGEST_LENGTH];
315 char *salt_out;
27545970
GT
316 int n;
317 unsigned int i;
dbad1690 318 EVP_MD_CTX md,md2;
e6e7b5f3
BM
319 size_t passwd_len, salt_len;
320
321 passwd_len = strlen(passwd);
1f4643a2
BM
322 out_buf[0] = '$';
323 out_buf[1] = 0;
324 assert(strlen(magic) <= 4); /* "1" or "apr1" */
325 strncat(out_buf, magic, 4);
326 strncat(out_buf, "$", 1);
e6e7b5f3
BM
327 strncat(out_buf, salt, 8);
328 assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
57108f0a 329 salt_out = out_buf + 2 + strlen(magic);
e6e7b5f3
BM
330 salt_len = strlen(salt_out);
331 assert(salt_len <= 8);
332
dbad1690 333 EVP_MD_CTX_init(&md);
20d2186c 334 EVP_DigestInit_ex(&md,EVP_md5(), NULL);
323f289c
DSH
335 EVP_DigestUpdate(&md, passwd, passwd_len);
336 EVP_DigestUpdate(&md, "$", 1);
337 EVP_DigestUpdate(&md, magic, strlen(magic));
338 EVP_DigestUpdate(&md, "$", 1);
339 EVP_DigestUpdate(&md, salt_out, salt_len);
e6e7b5f3 340
dbad1690 341 EVP_MD_CTX_init(&md2);
20d2186c 342 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
dbad1690
BL
343 EVP_DigestUpdate(&md2, passwd, passwd_len);
344 EVP_DigestUpdate(&md2, salt_out, salt_len);
345 EVP_DigestUpdate(&md2, passwd, passwd_len);
20d2186c 346 EVP_DigestFinal_ex(&md2, buf, NULL);
e6e7b5f3 347
e6e7b5f3 348 for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
323f289c
DSH
349 EVP_DigestUpdate(&md, buf, sizeof buf);
350 EVP_DigestUpdate(&md, buf, i);
e6e7b5f3
BM
351
352 n = passwd_len;
353 while (n)
354 {
323f289c 355 EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
e6e7b5f3
BM
356 n >>= 1;
357 }
20d2186c 358 EVP_DigestFinal_ex(&md, buf, NULL);
e6e7b5f3
BM
359
360 for (i = 0; i < 1000; i++)
361 {
20d2186c 362 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
7d727231 363 EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf,
323f289c 364 (i & 1) ? passwd_len : sizeof buf);
e6e7b5f3 365 if (i % 3)
323f289c 366 EVP_DigestUpdate(&md2, salt_out, salt_len);
e6e7b5f3 367 if (i % 7)
323f289c 368 EVP_DigestUpdate(&md2, passwd, passwd_len);
7d727231 369 EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd,
323f289c 370 (i & 1) ? sizeof buf : passwd_len);
20d2186c 371 EVP_DigestFinal_ex(&md2, buf, NULL);
e6e7b5f3 372 }
dbad1690 373 EVP_MD_CTX_cleanup(&md2);
e6e7b5f3
BM
374
375 {
376 /* transform buf into output string */
377
378 unsigned char buf_perm[sizeof buf];
379 int dest, source;
380 char *output;
381
382 /* silly output permutation */
383 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
384 buf_perm[dest] = buf[source];
385 buf_perm[14] = buf[5];
386 buf_perm[15] = buf[11];
bd445703 387#ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
e6e7b5f3 388 assert(16 == sizeof buf_perm);
bd445703 389#endif
e6e7b5f3
BM
390
391 output = salt_out + salt_len;
392 assert(output == out_buf + strlen(out_buf));
393
394 *output++ = '$';
395
396 for (i = 0; i < 15; i += 3)
397 {
398 *output++ = cov_2char[buf_perm[i+2] & 0x3f];
399 *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
400 (buf_perm[i+2] >> 6)];
401 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
402 (buf_perm[i+1] >> 4)];
403 *output++ = cov_2char[buf_perm[i] >> 2];
404 }
405 assert(i == 15);
406 *output++ = cov_2char[buf_perm[i] & 0x3f];
407 *output++ = cov_2char[buf_perm[i] >> 6];
408 *output = 0;
409 assert(strlen(out_buf) < sizeof(out_buf));
410 }
dbad1690 411 EVP_MD_CTX_cleanup(&md);
e6e7b5f3
BM
412
413 return out_buf;
414 }
bb325c7d 415#endif
e6e7b5f3
BM
416
417
418static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
419 char *passwd, BIO *out, int quiet, int table, int reverse,
1f4643a2 420 size_t pw_maxlen, int usecrypt, int use1, int useapr1)
e6e7b5f3
BM
421 {
422 char *hash = NULL;
423
424 assert(salt_p != NULL);
425 assert(salt_malloc_p != NULL);
426
427 /* first make sure we have a salt */
428 if (!passed_salt)
429 {
cf1b7d96 430#ifndef OPENSSL_NO_DES
e6e7b5f3
BM
431 if (usecrypt)
432 {
433 if (*salt_malloc_p == NULL)
434 {
26a3a48d 435 *salt_p = *salt_malloc_p = OPENSSL_malloc(3);
e6e7b5f3
BM
436 if (*salt_malloc_p == NULL)
437 goto err;
438 }
439 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
440 goto err;
441 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
442 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
443 (*salt_p)[2] = 0;
444#ifdef CHARSET_EBCDIC
445 ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
446 * back to ASCII */
447#endif
448 }
cf1b7d96 449#endif /* !OPENSSL_NO_DES */
e6e7b5f3 450
1f4643a2
BM
451#ifndef NO_MD5CRYPT_1
452 if (use1 || useapr1)
e6e7b5f3
BM
453 {
454 int i;
455
456 if (*salt_malloc_p == NULL)
457 {
26a3a48d 458 *salt_p = *salt_malloc_p = OPENSSL_malloc(9);
e6e7b5f3
BM
459 if (*salt_malloc_p == NULL)
460 goto err;
461 }
462 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
463 goto err;
464
465 for (i = 0; i < 8; i++)
466 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
467 (*salt_p)[8] = 0;
468 }
1f4643a2 469#endif /* !NO_MD5CRYPT_1 */
e6e7b5f3
BM
470 }
471
472 assert(*salt_p != NULL);
473
474 /* truncate password if necessary */
475 if ((strlen(passwd) > pw_maxlen))
476 {
477 if (!quiet)
a51a9726
BL
478 /* XXX: really we should know how to print a size_t, not cast it */
479 BIO_printf(bio_err, "Warning: truncating password to %u characters\n", (unsigned)pw_maxlen);
e6e7b5f3
BM
480 passwd[pw_maxlen] = 0;
481 }
482 assert(strlen(passwd) <= pw_maxlen);
483
484 /* now compute password hash */
cf1b7d96 485#ifndef OPENSSL_NO_DES
e6e7b5f3 486 if (usecrypt)
125cc35b 487 hash = DES_crypt(passwd, *salt_p);
e6e7b5f3 488#endif
1f4643a2
BM
489#ifndef NO_MD5CRYPT_1
490 if (use1 || useapr1)
491 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
e6e7b5f3
BM
492#endif
493 assert(hash != NULL);
494
495 if (table && !reverse)
496 BIO_printf(out, "%s\t%s\n", passwd, hash);
497 else if (table && reverse)
498 BIO_printf(out, "%s\t%s\n", hash, passwd);
499 else
500 BIO_printf(out, "%s\n", hash);
501 return 1;
502
503err:
504 return 0;
505 }
3ebf0be1 506#else
e6e7b5f3 507
3ebf0be1
BM
508int MAIN(int argc, char **argv)
509 {
510 fputs("Program not available.\n", stderr)
1c3e4a36 511 OPENSSL_EXIT(1);
3ebf0be1
BM
512 }
513#endif