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