a45245cd7ffb8f78cd1f37f13c371359676c012c
[openssl.git] / apps / passwd.c
1 /*
2  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
11 # define NO_MD5CRYPT_1
12 #endif
13
14 #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
15
16 # include <string.h>
17
18 # include "apps.h"
19
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/evp.h>
23 # include <openssl/rand.h>
24 # ifndef OPENSSL_NO_DES
25 #  include <openssl/des.h>
26 # endif
27 # ifndef NO_MD5CRYPT_1
28 #  include <openssl/md5.h>
29 # endif
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,
45                      int reverse, size_t pw_maxlen, int usecrypt, int use1,
46                      int useapr1);
47
48 typedef enum OPTION_choice {
49     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
50     OPT_IN,
51     OPT_NOVERIFY, OPT_QUIET, OPT_TABLE, OPT_REVERSE, OPT_APR1,
52     OPT_1, OPT_CRYPT, OPT_SALT, OPT_STDIN
53 } OPTION_CHOICE;
54
55 OPTIONS passwd_options[] = {
56     {"help", OPT_HELP, '-', "Display this summary"},
57     {"in", OPT_IN, '<', "Pead passwords from file"},
58     {"noverify", OPT_NOVERIFY, '-',
59      "Never verify when reading password from terminal"},
60     {"quiet", OPT_QUIET, '-', "No warnings"},
61     {"table", OPT_TABLE, '-', "Format output as table"},
62     {"reverse", OPT_REVERSE, '-', "Switch table columns"},
63     {"salt", OPT_SALT, 's', "Use provided salt"},
64     {"stdin", OPT_STDIN, '-', "Read passwords from stdin"},
65 # ifndef NO_MD5CRYPT_1
66     {"apr1", OPT_APR1, '-', "MD5-based password algorithm, Apache variant"},
67     {"1", OPT_1, '-', "MD5-based password algorithm"},
68 # endif
69 # ifndef OPENSSL_NO_DES
70     {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
71 # endif
72     {NULL}
73 };
74
75 int passwd_main(int argc, char **argv)
76 {
77     BIO *in = NULL;
78     char *infile = NULL, *salt = NULL, *passwd = NULL, **passwds = NULL;
79     char *salt_malloc = NULL, *passwd_malloc = NULL, *prog;
80     OPTION_CHOICE o;
81     int in_stdin = 0, pw_source_defined = 0;
82 #ifndef OPENSSL_NO_UI
83     int in_noverify = 0;
84 #endif
85     int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
86     int ret = 1, usecrypt = 0, use1 = 0, useapr1 = 0;
87     size_t passwd_malloc_size = 0, pw_maxlen = 256;
88
89     prog = opt_init(argc, argv, passwd_options);
90     while ((o = opt_next()) != OPT_EOF) {
91         switch (o) {
92         case OPT_EOF:
93         case OPT_ERR:
94  opthelp:
95             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
96             goto end;
97         case OPT_HELP:
98             opt_help(passwd_options);
99             ret = 0;
100             goto end;
101         case OPT_IN:
102             if (pw_source_defined)
103                 goto opthelp;
104             infile = opt_arg();
105             pw_source_defined = 1;
106             break;
107         case OPT_NOVERIFY:
108 #ifndef OPENSSL_NO_UI
109             in_noverify = 1;
110 #endif
111             break;
112         case OPT_QUIET:
113             quiet = 1;
114             break;
115         case OPT_TABLE:
116             table = 1;
117             break;
118         case OPT_REVERSE:
119             reverse = 1;
120             break;
121         case OPT_1:
122             use1 = 1;
123             break;
124         case OPT_APR1:
125             useapr1 = 1;
126             break;
127         case OPT_CRYPT:
128             usecrypt = 1;
129             break;
130         case OPT_SALT:
131             passed_salt = 1;
132             salt = opt_arg();
133             break;
134         case OPT_STDIN:
135             if (pw_source_defined)
136                 goto opthelp;
137             in_stdin = 1;
138             pw_source_defined = 1;
139             break;
140         }
141     }
142     argc = opt_num_rest();
143     argv = opt_rest();
144
145     if (*argv) {
146         if (pw_source_defined)
147             goto opthelp;
148         pw_source_defined = 1;
149         passwds = argv;
150     }
151
152     if (!usecrypt && !use1 && !useapr1) {
153         /* use default */
154         usecrypt = 1;
155     }
156     if (usecrypt + use1 + useapr1 > 1) {
157         /* conflict */
158         goto opthelp;
159     }
160
161 # ifdef OPENSSL_NO_DES
162     if (usecrypt)
163         goto opthelp;
164 # endif
165 # ifdef NO_MD5CRYPT_1
166     if (use1 || useapr1)
167         goto opthelp;
168 # endif
169
170     if (infile != NULL && in_stdin) {
171         BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog);
172         goto end;
173     }
174
175     if (infile != NULL || in_stdin) {
176         /*
177          * If in_stdin is true, we know that infile is NULL, and that
178          * bio_open_default() will give us back an alias for stdin.
179          */
180         in = bio_open_default(infile, 'r', FORMAT_TEXT);
181         if (in == NULL)
182             goto end;
183     }
184
185     if (usecrypt)
186         pw_maxlen = 8;
187     else if (use1 || useapr1)
188         pw_maxlen = 256;        /* arbitrary limit, should be enough for most
189                                  * passwords */
190
191     if (passwds == NULL) {
192         /* no passwords on the command line */
193
194         passwd_malloc_size = pw_maxlen + 2;
195         /* longer than necessary so that we can warn about truncation */
196         passwd = passwd_malloc =
197             app_malloc(passwd_malloc_size, "password buffer");
198     }
199
200     if ((in == NULL) && (passwds == NULL)) {
201         if (1) {
202 #ifndef OPENSSL_NO_UI
203             /* build a null-terminated list */
204             static char *passwds_static[2] = { NULL, NULL };
205
206             passwds = passwds_static;
207             if (in == NULL)
208                 if (EVP_read_pw_string
209                     (passwd_malloc, passwd_malloc_size, "Password: ",
210                      !(passed_salt || in_noverify)) != 0)
211                     goto end;
212             passwds[0] = passwd_malloc;
213         } else {
214 #endif
215             BIO_printf(bio_err, "password required\n");
216             goto end;
217         }
218     }
219
220
221     if (in == NULL) {
222         assert(passwds != NULL);
223         assert(*passwds != NULL);
224
225         do {                    /* loop over list of passwords */
226             passwd = *passwds++;
227             if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
228                            quiet, table, reverse, pw_maxlen, usecrypt, use1,
229                            useapr1))
230                 goto end;
231         }
232         while (*passwds != NULL);
233     } else
234         /* in != NULL */
235     {
236         int done;
237
238         assert(passwd != NULL);
239         do {
240             int r = BIO_gets(in, passwd, pw_maxlen + 1);
241             if (r > 0) {
242                 char *c = (strchr(passwd, '\n'));
243                 if (c != NULL)
244                     *c = 0;     /* truncate at newline */
245                 else {
246                     /* ignore rest of line */
247                     char trash[BUFSIZ];
248                     do
249                         r = BIO_gets(in, trash, sizeof trash);
250                     while ((r > 0) && (!strchr(trash, '\n')));
251                 }
252
253                 if (!do_passwd
254                     (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
255                      table, reverse, pw_maxlen, usecrypt, use1, useapr1))
256                     goto end;
257             }
258             done = (r <= 0);
259         }
260         while (!done);
261     }
262     ret = 0;
263
264  end:
265     ERR_print_errors(bio_err);
266     OPENSSL_free(salt_malloc);
267     OPENSSL_free(passwd_malloc);
268     BIO_free(in);
269     return (ret);
270 }
271
272 # ifndef NO_MD5CRYPT_1
273 /*
274  * MD5-based password algorithm (should probably be available as a library
275  * function; then the static buffer would not be acceptable). For magic
276  * string "1", this should be compatible to the MD5-based BSD password
277  * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
278  * Apache password algorithm. (Apparently, the Apache password algorithm is
279  * identical except that the 'magic' string was changed -- the laziest
280  * application of the NIH principle I've ever encountered.)
281  */
282 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
283 {
284     /* "$apr1$..salt..$.......md5hash..........\0" */
285     static char out_buf[6 + 9 + 24 + 2];
286     unsigned char buf[MD5_DIGEST_LENGTH];
287     char *salt_out;
288     int n;
289     unsigned int i;
290     EVP_MD_CTX *md = NULL, *md2 = NULL;
291     size_t passwd_len, salt_len, magic_len;
292
293     passwd_len = strlen(passwd);
294     out_buf[0] = '$';
295     out_buf[1] = 0;
296     magic_len = strlen(magic);
297
298     if (magic_len > 4)    /* assert it's  "1" or "apr1" */
299         return NULL;
300
301     OPENSSL_strlcat(out_buf, magic, sizeof out_buf);
302     OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
303     OPENSSL_strlcat(out_buf, salt, sizeof out_buf);
304
305     if (strlen(out_buf) > 6 + 8) /* assert "$apr1$..salt.." */
306         return NULL;
307
308     salt_out = out_buf + 2 + magic_len;
309     salt_len = strlen(salt_out);
310
311     if (salt_len > 8)
312         return NULL;
313
314     md = EVP_MD_CTX_new();
315     if (md == NULL
316         || !EVP_DigestInit_ex(md, EVP_md5(), NULL)
317         || !EVP_DigestUpdate(md, passwd, passwd_len)
318         || !EVP_DigestUpdate(md, "$", 1)
319         || !EVP_DigestUpdate(md, magic, magic_len)
320         || !EVP_DigestUpdate(md, "$", 1)
321         || !EVP_DigestUpdate(md, salt_out, salt_len))
322         goto err;
323
324     md2 = EVP_MD_CTX_new();
325     if (md2 == NULL
326         || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
327         || !EVP_DigestUpdate(md2, passwd, passwd_len)
328         || !EVP_DigestUpdate(md2, salt_out, salt_len)
329         || !EVP_DigestUpdate(md2, passwd, passwd_len)
330         || !EVP_DigestFinal_ex(md2, buf, NULL))
331         goto err;
332
333     for (i = passwd_len; i > sizeof buf; i -= sizeof buf) {
334         if (!EVP_DigestUpdate(md, buf, sizeof buf))
335             goto err;
336     }
337     if (!EVP_DigestUpdate(md, buf, i))
338         goto err;
339
340     n = passwd_len;
341     while (n) {
342         if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
343             goto err;
344         n >>= 1;
345     }
346     if (!EVP_DigestFinal_ex(md, buf, NULL))
347         return NULL;
348
349     for (i = 0; i < 1000; i++) {
350         if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
351             goto err;
352         if (!EVP_DigestUpdate(md2,
353                               (i & 1) ? (unsigned const char *)passwd : buf,
354                               (i & 1) ? passwd_len : sizeof buf))
355             goto err;
356         if (i % 3) {
357             if (!EVP_DigestUpdate(md2, salt_out, salt_len))
358                 goto err;
359         }
360         if (i % 7) {
361             if (!EVP_DigestUpdate(md2, passwd, passwd_len))
362                 goto err;
363         }
364         if (!EVP_DigestUpdate(md2,
365                               (i & 1) ? buf : (unsigned const char *)passwd,
366                               (i & 1) ? sizeof buf : passwd_len))
367                 goto err;
368         if (!EVP_DigestFinal_ex(md2, buf, NULL))
369                 goto err;
370     }
371     EVP_MD_CTX_free(md2);
372     EVP_MD_CTX_free(md);
373     md2 = NULL;
374     md = NULL;
375
376     {
377         /* transform buf into output string */
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;
384              dest++, source = (source + 6) % 17)
385             buf_perm[dest] = buf[source];
386         buf_perm[14] = buf[5];
387         buf_perm[15] = buf[11];
388 #  ifndef PEDANTIC              /* Unfortunately, this generates a "no
389                                  * effect" warning */
390         assert(16 == sizeof buf_perm);
391 #  endif
392
393         output = salt_out + salt_len;
394         assert(output == out_buf + strlen(out_buf));
395
396         *output++ = '$';
397
398         for (i = 0; i < 15; i += 3) {
399             *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
400             *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
401                                   (buf_perm[i + 2] >> 6)];
402             *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
403                                   (buf_perm[i + 1] >> 4)];
404             *output++ = cov_2char[buf_perm[i] >> 2];
405         }
406         assert(i == 15);
407         *output++ = cov_2char[buf_perm[i] & 0x3f];
408         *output++ = cov_2char[buf_perm[i] >> 6];
409         *output = 0;
410         assert(strlen(out_buf) < sizeof(out_buf));
411     }
412
413     return out_buf;
414
415  err:
416     EVP_MD_CTX_free(md2);
417     EVP_MD_CTX_free(md);
418     return NULL;
419 }
420 # endif
421
422 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
423                      char *passwd, BIO *out, int quiet, int table,
424                      int reverse, size_t pw_maxlen, int usecrypt, int use1,
425                      int useapr1)
426 {
427     char *hash = NULL;
428
429     assert(salt_p != NULL);
430     assert(salt_malloc_p != NULL);
431
432     /* first make sure we have a salt */
433     if (!passed_salt) {
434 # ifndef OPENSSL_NO_DES
435         if (usecrypt) {
436             if (*salt_malloc_p == NULL) {
437                 *salt_p = *salt_malloc_p = app_malloc(3, "salt buffer");
438             }
439             if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
440                 goto end;
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 back
446                                                 * to ASCII */
447 #  endif
448         }
449 # endif                         /* !OPENSSL_NO_DES */
450
451 # ifndef NO_MD5CRYPT_1
452         if (use1 || useapr1) {
453             int i;
454
455             if (*salt_malloc_p == NULL) {
456                 *salt_p = *salt_malloc_p = app_malloc(9, "salt buffer");
457             }
458             if (RAND_bytes((unsigned char *)*salt_p, 8) <= 0)
459                 goto end;
460
461             for (i = 0; i < 8; i++)
462                 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
463             (*salt_p)[8] = 0;
464         }
465 # endif                         /* !NO_MD5CRYPT_1 */
466     }
467
468     assert(*salt_p != NULL);
469
470     /* truncate password if necessary */
471     if ((strlen(passwd) > pw_maxlen)) {
472         if (!quiet)
473             /*
474              * XXX: really we should know how to print a size_t, not cast it
475              */
476             BIO_printf(bio_err,
477                        "Warning: truncating password to %u characters\n",
478                        (unsigned)pw_maxlen);
479         passwd[pw_maxlen] = 0;
480     }
481     assert(strlen(passwd) <= pw_maxlen);
482
483     /* now compute password hash */
484 # ifndef OPENSSL_NO_DES
485     if (usecrypt)
486         hash = DES_crypt(passwd, *salt_p);
487 # endif
488 # ifndef NO_MD5CRYPT_1
489     if (use1 || useapr1)
490         hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
491 # endif
492     assert(hash != NULL);
493
494     if (table && !reverse)
495         BIO_printf(out, "%s\t%s\n", passwd, hash);
496     else if (table && reverse)
497         BIO_printf(out, "%s\t%s\n", hash, passwd);
498     else
499         BIO_printf(out, "%s\n", hash);
500     return 1;
501
502  end:
503     return 0;
504 }
505 #else
506
507 int passwd_main(int argc, char **argv)
508 {
509     BIO_printf(bio_err, "Program not available.\n");
510     return (1);
511 }
512 #endif