Previously this x509 command line was working, restore that
[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, '<', "Read 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         /*
202          * we use the following method to make sure what
203          * in the 'else' section is always compiled, to
204          * avoid rot of not-frequently-used code.
205          */
206         if (1) {
207 # ifndef OPENSSL_NO_UI
208             /* build a null-terminated list */
209             static char *passwds_static[2] = { NULL, NULL };
210
211             passwds = passwds_static;
212             if (in == NULL) {
213                 if (EVP_read_pw_string
214                     (passwd_malloc, passwd_malloc_size, "Password: ",
215                      !(passed_salt || in_noverify)) != 0)
216                     goto end;
217             }
218             passwds[0] = passwd_malloc;
219         } else {
220 # endif
221             BIO_printf(bio_err, "password required\n");
222             goto end;
223         }
224     }
225
226     if (in == NULL) {
227         assert(passwds != NULL);
228         assert(*passwds != NULL);
229
230         do {                    /* loop over list of passwords */
231             passwd = *passwds++;
232             if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
233                            quiet, table, reverse, pw_maxlen, usecrypt, use1,
234                            useapr1))
235                 goto end;
236         } while (*passwds != NULL);
237     } else {
238         /* in != NULL */
239         int done;
240
241         assert(passwd != NULL);
242         do {
243             int r = BIO_gets(in, passwd, pw_maxlen + 1);
244             if (r > 0) {
245                 char *c = (strchr(passwd, '\n'));
246                 if (c != NULL) {
247                     *c = 0;     /* truncate at newline */
248                 } else {
249                     /* ignore rest of line */
250                     char trash[BUFSIZ];
251                     do
252                         r = BIO_gets(in, trash, sizeof(trash));
253                     while ((r > 0) && (!strchr(trash, '\n')));
254                 }
255
256                 if (!do_passwd
257                     (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
258                      table, reverse, pw_maxlen, usecrypt, use1, useapr1))
259                     goto end;
260             }
261             done = (r <= 0);
262         } while (!done);
263     }
264     ret = 0;
265
266  end:
267     ERR_print_errors(bio_err);
268     OPENSSL_free(salt_malloc);
269     OPENSSL_free(passwd_malloc);
270     BIO_free(in);
271     return (ret);
272 }
273
274 # ifndef NO_MD5CRYPT_1
275 /*
276  * MD5-based password algorithm (should probably be available as a library
277  * function; then the static buffer would not be acceptable). For magic
278  * string "1", this should be compatible to the MD5-based BSD password
279  * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
280  * Apache password algorithm. (Apparently, the Apache password algorithm is
281  * identical except that the 'magic' string was changed -- the laziest
282  * application of the NIH principle I've ever encountered.)
283  */
284 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
285 {
286     /* "$apr1$..salt..$.......md5hash..........\0" */
287     static char out_buf[6 + 9 + 24 + 2];
288     unsigned char buf[MD5_DIGEST_LENGTH];
289     char *salt_out;
290     int n;
291     unsigned int i;
292     EVP_MD_CTX *md = NULL, *md2 = NULL;
293     size_t passwd_len, salt_len, magic_len;
294
295     passwd_len = strlen(passwd);
296     out_buf[0] = '$';
297     out_buf[1] = 0;
298     magic_len = strlen(magic);
299
300     if (magic_len > 4)    /* assert it's  "1" or "apr1" */
301         return NULL;
302
303     OPENSSL_strlcat(out_buf, magic, sizeof(out_buf));
304     OPENSSL_strlcat(out_buf, "$", sizeof(out_buf));
305     OPENSSL_strlcat(out_buf, salt, sizeof(out_buf));
306
307     if (strlen(out_buf) > 6 + 8) /* assert "$apr1$..salt.." */
308         return NULL;
309
310     salt_out = out_buf + 2 + magic_len;
311     salt_len = strlen(salt_out);
312
313     if (salt_len > 8)
314         return NULL;
315
316     md = EVP_MD_CTX_new();
317     if (md == NULL
318         || !EVP_DigestInit_ex(md, EVP_md5(), NULL)
319         || !EVP_DigestUpdate(md, passwd, passwd_len)
320         || !EVP_DigestUpdate(md, "$", 1)
321         || !EVP_DigestUpdate(md, magic, magic_len)
322         || !EVP_DigestUpdate(md, "$", 1)
323         || !EVP_DigestUpdate(md, salt_out, salt_len))
324         goto err;
325
326     md2 = EVP_MD_CTX_new();
327     if (md2 == NULL
328         || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
329         || !EVP_DigestUpdate(md2, passwd, passwd_len)
330         || !EVP_DigestUpdate(md2, salt_out, salt_len)
331         || !EVP_DigestUpdate(md2, passwd, passwd_len)
332         || !EVP_DigestFinal_ex(md2, buf, NULL))
333         goto err;
334
335     for (i = passwd_len; i > sizeof(buf); i -= sizeof(buf)) {
336         if (!EVP_DigestUpdate(md, buf, sizeof(buf)))
337             goto err;
338     }
339     if (!EVP_DigestUpdate(md, buf, i))
340         goto err;
341
342     n = passwd_len;
343     while (n) {
344         if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
345             goto err;
346         n >>= 1;
347     }
348     if (!EVP_DigestFinal_ex(md, buf, NULL))
349         return NULL;
350
351     for (i = 0; i < 1000; i++) {
352         if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
353             goto err;
354         if (!EVP_DigestUpdate(md2,
355                               (i & 1) ? (unsigned const char *)passwd : buf,
356                               (i & 1) ? passwd_len : sizeof(buf)))
357             goto err;
358         if (i % 3) {
359             if (!EVP_DigestUpdate(md2, salt_out, salt_len))
360                 goto err;
361         }
362         if (i % 7) {
363             if (!EVP_DigestUpdate(md2, passwd, passwd_len))
364                 goto err;
365         }
366         if (!EVP_DigestUpdate(md2,
367                               (i & 1) ? buf : (unsigned const char *)passwd,
368                               (i & 1) ? sizeof(buf) : passwd_len))
369                 goto err;
370         if (!EVP_DigestFinal_ex(md2, buf, NULL))
371                 goto err;
372     }
373     EVP_MD_CTX_free(md2);
374     EVP_MD_CTX_free(md);
375     md2 = NULL;
376     md = NULL;
377
378     {
379         /* transform buf into output string */
380         unsigned char buf_perm[sizeof(buf)];
381         int dest, source;
382         char *output;
383
384         /* silly output permutation */
385         for (dest = 0, source = 0; dest < 14;
386              dest++, source = (source + 6) % 17)
387             buf_perm[dest] = buf[source];
388         buf_perm[14] = buf[5];
389         buf_perm[15] = buf[11];
390 #  ifndef PEDANTIC              /* Unfortunately, this generates a "no
391                                  * effect" warning */
392         assert(16 == sizeof(buf_perm));
393 #  endif
394
395         output = salt_out + salt_len;
396         assert(output == out_buf + strlen(out_buf));
397
398         *output++ = '$';
399
400         for (i = 0; i < 15; i += 3) {
401             *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
402             *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
403                                   (buf_perm[i + 2] >> 6)];
404             *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
405                                   (buf_perm[i + 1] >> 4)];
406             *output++ = cov_2char[buf_perm[i] >> 2];
407         }
408         assert(i == 15);
409         *output++ = cov_2char[buf_perm[i] & 0x3f];
410         *output++ = cov_2char[buf_perm[i] >> 6];
411         *output = 0;
412         assert(strlen(out_buf) < sizeof(out_buf));
413     }
414
415     return out_buf;
416
417  err:
418     EVP_MD_CTX_free(md2);
419     EVP_MD_CTX_free(md);
420     return NULL;
421 }
422 # endif
423
424 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
425                      char *passwd, BIO *out, int quiet, int table,
426                      int reverse, size_t pw_maxlen, int usecrypt, int use1,
427                      int useapr1)
428 {
429     char *hash = NULL;
430
431     assert(salt_p != NULL);
432     assert(salt_malloc_p != NULL);
433
434     /* first make sure we have a salt */
435     if (!passed_salt) {
436 # ifndef OPENSSL_NO_DES
437         if (usecrypt) {
438             if (*salt_malloc_p == NULL)
439                 *salt_p = *salt_malloc_p = app_malloc(3, "salt buffer");
440             if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
441                 goto end;
442             (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
443             (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
444             (*salt_p)[2] = 0;
445 #  ifdef CHARSET_EBCDIC
446             ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert back
447                                                 * to ASCII */
448 #  endif
449         }
450 # endif                         /* !OPENSSL_NO_DES */
451
452 # ifndef NO_MD5CRYPT_1
453         if (use1 || useapr1) {
454             int i;
455
456             if (*salt_malloc_p == NULL)
457                 *salt_p = *salt_malloc_p = app_malloc(9, "salt buffer");
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