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