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