Fix some pedantic warnings.
[openssl.git] / apps / passwd.c
1 /*
2  * Copyright 2000-2017 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_SHA || defined CHARSET_EBCDIC
15 # define NO_SHACRYPT
16 #endif
17
18 #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1) || !defined(NO_SHACRYPT)
19
20 # include <string.h>
21
22 # include "apps.h"
23
24 # include <openssl/bio.h>
25 # include <openssl/err.h>
26 # include <openssl/evp.h>
27 # include <openssl/rand.h>
28 # ifndef OPENSSL_NO_DES
29 #  include <openssl/des.h>
30 # endif
31 # ifndef NO_MD5CRYPT_1
32 #  include <openssl/md5.h>
33 # endif
34 # ifndef NO_SHACRYPT
35 #  include <openssl/sha.h>
36 # endif
37
38 static unsigned const char cov_2char[64] = {
39     /* from crypto/des/fcrypt.c */
40     0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
41     0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
42     0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
43     0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
44     0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62,
45     0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
46     0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
47     0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
48 };
49
50 typedef enum {
51     passwd_unset = 0,
52     passwd_crypt,
53     passwd_md5,
54     passwd_apr1,
55     passwd_sha256,
56     passwd_sha512,
57     passwd_aixmd5
58 } passwd_modes;
59
60 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
61                      char *passwd, BIO *out, int quiet, int table,
62                      int reverse, size_t pw_maxlen, passwd_modes mode);
63
64 typedef enum OPTION_choice {
65     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
66     OPT_IN,
67     OPT_NOVERIFY, OPT_QUIET, OPT_TABLE, OPT_REVERSE, OPT_APR1,
68     OPT_1, OPT_5, OPT_6, OPT_CRYPT, OPT_AIXMD5, OPT_SALT, OPT_STDIN,
69     OPT_R_ENUM
70 } OPTION_CHOICE;
71
72 const OPTIONS passwd_options[] = {
73     {"help", OPT_HELP, '-', "Display this summary"},
74     {"in", OPT_IN, '<', "Read passwords from file"},
75     {"noverify", OPT_NOVERIFY, '-',
76      "Never verify when reading password from terminal"},
77     {"quiet", OPT_QUIET, '-', "No warnings"},
78     {"table", OPT_TABLE, '-', "Format output as table"},
79     {"reverse", OPT_REVERSE, '-', "Switch table columns"},
80     {"salt", OPT_SALT, 's', "Use provided salt"},
81     {"stdin", OPT_STDIN, '-', "Read passwords from stdin"},
82 # ifndef NO_SHACRYPT
83     {"6", OPT_6, '-', "SHA512-based password algorithm"},
84     {"5", OPT_5, '-', "SHA256-based password algorithm"},
85 # endif
86 # ifndef NO_MD5CRYPT_1
87     {"apr1", OPT_APR1, '-', "MD5-based password algorithm, Apache variant"},
88     {"1", OPT_1, '-', "MD5-based password algorithm"},
89     {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
90 # endif
91 # ifndef OPENSSL_NO_DES
92     {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
93 # endif
94     OPT_R_OPTIONS,
95     {NULL}
96 };
97
98 int passwd_main(int argc, char **argv)
99 {
100     BIO *in = NULL;
101     char *infile = NULL, *salt = NULL, *passwd = NULL, **passwds = NULL;
102     char *salt_malloc = NULL, *passwd_malloc = NULL, *prog;
103     OPTION_CHOICE o;
104     int in_stdin = 0, pw_source_defined = 0;
105 # ifndef OPENSSL_NO_UI_CONSOLE
106     int in_noverify = 0;
107 # endif
108     int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
109     int ret = 1;
110     passwd_modes mode = passwd_unset;
111     size_t passwd_malloc_size = 0;
112     size_t pw_maxlen = 256; /* arbitrary limit, should be enough for most
113                              * passwords */
114
115     prog = opt_init(argc, argv, passwd_options);
116     while ((o = opt_next()) != OPT_EOF) {
117         switch (o) {
118         case OPT_EOF:
119         case OPT_ERR:
120  opthelp:
121             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
122             goto end;
123         case OPT_HELP:
124             opt_help(passwd_options);
125             ret = 0;
126             goto end;
127         case OPT_IN:
128             if (pw_source_defined)
129                 goto opthelp;
130             infile = opt_arg();
131             pw_source_defined = 1;
132             break;
133         case OPT_NOVERIFY:
134 # ifndef OPENSSL_NO_UI_CONSOLE
135             in_noverify = 1;
136 # endif
137             break;
138         case OPT_QUIET:
139             quiet = 1;
140             break;
141         case OPT_TABLE:
142             table = 1;
143             break;
144         case OPT_REVERSE:
145             reverse = 1;
146             break;
147         case OPT_1:
148             if (mode != passwd_unset)
149                 goto opthelp;
150             mode = passwd_md5;
151             break;
152         case OPT_5:
153             if (mode != passwd_unset)
154                 goto opthelp;
155             mode = passwd_sha256;
156             break;
157         case OPT_6:
158             if (mode != passwd_unset)
159                 goto opthelp;
160             mode = passwd_sha512;
161             break;
162         case OPT_APR1:
163             if (mode != passwd_unset)
164                 goto opthelp;
165             mode = passwd_apr1;
166             break;
167         case OPT_AIXMD5:
168             if (mode != passwd_unset)
169                 goto opthelp;
170             mode = passwd_aixmd5;
171             break;
172         case OPT_CRYPT:
173             if (mode != passwd_unset)
174                 goto opthelp;
175             mode = passwd_crypt;
176             break;
177         case OPT_SALT:
178             passed_salt = 1;
179             salt = opt_arg();
180             break;
181         case OPT_STDIN:
182             if (pw_source_defined)
183                 goto opthelp;
184             in_stdin = 1;
185             pw_source_defined = 1;
186             break;
187         case OPT_R_CASES:
188             if (!opt_rand(o))
189                 goto end;
190             break;
191         }
192     }
193     argc = opt_num_rest();
194     argv = opt_rest();
195
196     if (*argv != NULL) {
197         if (pw_source_defined)
198             goto opthelp;
199         pw_source_defined = 1;
200         passwds = argv;
201     }
202
203     if (mode == passwd_unset) {
204         /* use default */
205         mode = passwd_crypt;
206     }
207
208 # ifdef OPENSSL_NO_DES
209     if (mode == passwd_crypt)
210         goto opthelp;
211 # endif
212 # ifdef NO_MD5CRYPT_1
213     if (mode == passwd_md5 || mode == passwd_apr1 || mode == passwd_aixmd5)
214         goto opthelp;
215 # endif
216 # ifdef NO_SHACRYPT
217     if (mode == passwd_sha256 || mode == passwd_sha512)
218         goto opthelp;
219 # endif
220
221     if (infile != NULL && in_stdin) {
222         BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog);
223         goto end;
224     }
225
226     if (infile != NULL || in_stdin) {
227         /*
228          * If in_stdin is true, we know that infile is NULL, and that
229          * bio_open_default() will give us back an alias for stdin.
230          */
231         in = bio_open_default(infile, 'r', FORMAT_TEXT);
232         if (in == NULL)
233             goto end;
234     }
235
236     if (mode == passwd_crypt)
237         pw_maxlen = 8;
238
239     if (passwds == NULL) {
240         /* no passwords on the command line */
241
242         passwd_malloc_size = pw_maxlen + 2;
243         /* longer than necessary so that we can warn about truncation */
244         passwd = passwd_malloc =
245             app_malloc(passwd_malloc_size, "password buffer");
246     }
247
248     if ((in == NULL) && (passwds == NULL)) {
249         /*
250          * we use the following method to make sure what
251          * in the 'else' section is always compiled, to
252          * avoid rot of not-frequently-used code.
253          */
254         if (1) {
255 # ifndef OPENSSL_NO_UI_CONSOLE
256             /* build a null-terminated list */
257             static char *passwds_static[2] = { NULL, NULL };
258
259             passwds = passwds_static;
260             if (in == NULL) {
261                 if (EVP_read_pw_string
262                     (passwd_malloc, passwd_malloc_size, "Password: ",
263                      !(passed_salt || in_noverify)) != 0)
264                     goto end;
265             }
266             passwds[0] = passwd_malloc;
267         } else {
268 # endif
269             BIO_printf(bio_err, "password required\n");
270             goto end;
271         }
272     }
273
274     if (in == NULL) {
275         assert(passwds != NULL);
276         assert(*passwds != NULL);
277
278         do {                    /* loop over list of passwords */
279             passwd = *passwds++;
280             if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
281                            quiet, table, reverse, pw_maxlen, mode))
282                 goto end;
283         } while (*passwds != NULL);
284     } else {
285         /* in != NULL */
286         int done;
287
288         assert(passwd != NULL);
289         do {
290             int r = BIO_gets(in, passwd, pw_maxlen + 1);
291             if (r > 0) {
292                 char *c = (strchr(passwd, '\n'));
293                 if (c != NULL) {
294                     *c = 0;     /* truncate at newline */
295                 } else {
296                     /* ignore rest of line */
297                     char trash[BUFSIZ];
298                     do
299                         r = BIO_gets(in, trash, sizeof trash);
300                     while ((r > 0) && (!strchr(trash, '\n')));
301                 }
302
303                 if (!do_passwd
304                     (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
305                      table, reverse, pw_maxlen, mode))
306                     goto end;
307             }
308             done = (r <= 0);
309         } while (!done);
310     }
311     ret = 0;
312
313  end:
314     ERR_print_errors(bio_err);
315     OPENSSL_free(salt_malloc);
316     OPENSSL_free(passwd_malloc);
317     BIO_free(in);
318     return (ret);
319 }
320
321 # ifndef NO_MD5CRYPT_1
322 /*
323  * MD5-based password algorithm (should probably be available as a library
324  * function; then the static buffer would not be acceptable). For magic
325  * string "1", this should be compatible to the MD5-based BSD password
326  * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
327  * Apache password algorithm. (Apparently, the Apache password algorithm is
328  * identical except that the 'magic' string was changed -- the laziest
329  * application of the NIH principle I've ever encountered.)
330  */
331 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
332 {
333     /* "$apr1$..salt..$.......md5hash..........\0" */
334     static char out_buf[6 + 9 + 24 + 2];
335     unsigned char buf[MD5_DIGEST_LENGTH];
336     char *salt_out;
337     int n;
338     unsigned int i;
339     EVP_MD_CTX *md = NULL, *md2 = NULL;
340     size_t passwd_len, salt_len, magic_len;
341
342     passwd_len = strlen(passwd);
343
344     out_buf[0] = 0;
345     magic_len = strlen(magic);
346
347     if (magic_len > 0) {
348         out_buf[0] = '$';
349         out_buf[1] = 0;
350
351         if (magic_len > 4)    /* assert it's  "1" or "apr1" */
352             return NULL;
353
354         OPENSSL_strlcat(out_buf, magic, sizeof out_buf);
355         OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
356     }
357
358     OPENSSL_strlcat(out_buf, salt, sizeof out_buf);
359
360     if (strlen(out_buf) > 6 + 8) /* assert "$apr1$..salt.." */
361         return NULL;
362
363     salt_out = out_buf;
364     if (magic_len > 0)
365         salt_out += 2 + magic_len;
366     salt_len = strlen(salt_out);
367
368     if (salt_len > 8)
369         return NULL;
370
371     md = EVP_MD_CTX_new();
372     if (md == NULL
373         || !EVP_DigestInit_ex(md, EVP_md5(), NULL)
374         || !EVP_DigestUpdate(md, passwd, passwd_len))
375         goto err;
376
377     if (magic_len > 0)
378         if (!EVP_DigestUpdate(md, "$", 1)
379             || !EVP_DigestUpdate(md, magic, magic_len)
380             || !EVP_DigestUpdate(md, "$", 1))
381           goto err;
382
383     if (!EVP_DigestUpdate(md, salt_out, salt_len))
384         goto err;
385
386     md2 = EVP_MD_CTX_new();
387     if (md2 == NULL
388         || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
389         || !EVP_DigestUpdate(md2, passwd, passwd_len)
390         || !EVP_DigestUpdate(md2, salt_out, salt_len)
391         || !EVP_DigestUpdate(md2, passwd, passwd_len)
392         || !EVP_DigestFinal_ex(md2, buf, NULL))
393         goto err;
394
395     for (i = passwd_len; i > sizeof buf; i -= sizeof buf) {
396         if (!EVP_DigestUpdate(md, buf, sizeof buf))
397             goto err;
398     }
399     if (!EVP_DigestUpdate(md, buf, i))
400         goto err;
401
402     n = passwd_len;
403     while (n) {
404         if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
405             goto err;
406         n >>= 1;
407     }
408     if (!EVP_DigestFinal_ex(md, buf, NULL))
409         return NULL;
410
411     for (i = 0; i < 1000; i++) {
412         if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
413             goto err;
414         if (!EVP_DigestUpdate(md2,
415                               (i & 1) ? (unsigned const char *)passwd : buf,
416                               (i & 1) ? passwd_len : sizeof buf))
417             goto err;
418         if (i % 3) {
419             if (!EVP_DigestUpdate(md2, salt_out, salt_len))
420                 goto err;
421         }
422         if (i % 7) {
423             if (!EVP_DigestUpdate(md2, passwd, passwd_len))
424                 goto err;
425         }
426         if (!EVP_DigestUpdate(md2,
427                               (i & 1) ? buf : (unsigned const char *)passwd,
428                               (i & 1) ? sizeof buf : passwd_len))
429                 goto err;
430         if (!EVP_DigestFinal_ex(md2, buf, NULL))
431                 goto err;
432     }
433     EVP_MD_CTX_free(md2);
434     EVP_MD_CTX_free(md);
435     md2 = NULL;
436     md = NULL;
437
438     {
439         /* transform buf into output string */
440         unsigned char buf_perm[sizeof buf];
441         int dest, source;
442         char *output;
443
444         /* silly output permutation */
445         for (dest = 0, source = 0; dest < 14;
446              dest++, source = (source + 6) % 17)
447             buf_perm[dest] = buf[source];
448         buf_perm[14] = buf[5];
449         buf_perm[15] = buf[11];
450 #  ifndef PEDANTIC              /* Unfortunately, this generates a "no
451                                  * effect" warning */
452         assert(16 == sizeof buf_perm);
453 #  endif
454
455         output = salt_out + salt_len;
456         assert(output == out_buf + strlen(out_buf));
457
458         *output++ = '$';
459
460         for (i = 0; i < 15; i += 3) {
461             *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
462             *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
463                                   (buf_perm[i + 2] >> 6)];
464             *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
465                                   (buf_perm[i + 1] >> 4)];
466             *output++ = cov_2char[buf_perm[i] >> 2];
467         }
468         assert(i == 15);
469         *output++ = cov_2char[buf_perm[i] & 0x3f];
470         *output++ = cov_2char[buf_perm[i] >> 6];
471         *output = 0;
472         assert(strlen(out_buf) < sizeof(out_buf));
473     }
474
475     return out_buf;
476
477  err:
478     EVP_MD_CTX_free(md2);
479     EVP_MD_CTX_free(md);
480     return NULL;
481 }
482 # endif
483
484 # ifndef NO_SHACRYPT
485 /*
486  * SHA based password algorithm, describe by Ulrich Drepper here:
487  * https://www.akkadia.org/drepper/SHA-crypt.txt
488  * (note that it's in the public domain)
489  */
490 static char *shacrypt(const char *passwd, const char *magic, const char *salt)
491 {
492     /* Prefix for optional rounds specification.  */
493     static const char rounds_prefix[] = "rounds=";
494     /* Maximum salt string length.  */
495 #  define SALT_LEN_MAX 16
496     /* Default number of rounds if not explicitly specified.  */
497 #  define ROUNDS_DEFAULT 5000
498     /* Minimum number of rounds.  */
499 #  define ROUNDS_MIN 1000
500     /* Maximum number of rounds.  */
501 #  define ROUNDS_MAX 999999999
502
503     /* "$6$rounds=<N>$......salt......$...shahash(up to 86 chars)...\0" */
504     static char out_buf[3 + 17 + 17 + 86 + 1];
505     unsigned char buf[SHA512_DIGEST_LENGTH];
506     unsigned char temp_buf[SHA512_DIGEST_LENGTH];
507     size_t buf_size = 0;
508     char salt_copy[17];          /* Max 16 chars plus '\0' */
509     size_t n;
510     EVP_MD_CTX *md = NULL, *md2 = NULL;
511     const EVP_MD *sha = NULL;
512     size_t passwd_len, salt_len, magic_len;
513     unsigned int rounds = 5000;        /* Default */
514     char rounds_custom = 0;
515     char *p_bytes = NULL;
516     char *s_bytes = NULL;
517     char *cp = NULL;
518
519     passwd_len = strlen(passwd);
520     magic_len = strlen(magic);
521
522     /* assert it's "5" or "6" */
523     if (magic_len != 1)
524         return NULL;
525
526     switch (magic[0]) {
527     case '5':
528         sha = EVP_sha256();
529         buf_size = 32;
530         break;
531     case '6':
532         sha = EVP_sha512();
533         buf_size = 64;
534         break;
535     default:
536         return NULL;
537     }
538
539     if (strncmp(salt, rounds_prefix, sizeof(rounds_prefix) - 1) == 0) {
540         const char *num = salt + sizeof(rounds_prefix) - 1;
541         char *endp;
542         unsigned long int srounds = strtoul (num, &endp, 10);
543         if (*endp == '$') {
544             salt = endp + 1;
545             if (srounds > ROUNDS_MAX)
546                 rounds = ROUNDS_MAX;
547             else if (srounds < ROUNDS_MIN)
548                 rounds = ROUNDS_MIN;
549             else
550                 rounds = (unsigned int)srounds;
551             rounds_custom = 1;
552         } else {
553             return NULL;
554         }
555     }
556
557     /* The salt gets truncated to 16 chars */
558     OPENSSL_strlcpy(salt_copy, salt, sizeof salt_copy);
559     salt_len = strlen(salt_copy);
560
561     out_buf[0] = 0;
562     OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
563     OPENSSL_strlcat(out_buf, magic, sizeof out_buf);
564     OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
565     if (rounds_custom) {
566         char tmp_buf[80]; /* "rounds=999999999" */
567         sprintf(tmp_buf, "rounds=%u", rounds);
568         OPENSSL_strlcat(out_buf, tmp_buf, sizeof out_buf);
569         OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
570     }
571     OPENSSL_strlcat(out_buf, salt_copy, sizeof out_buf);
572
573     /* assert "$5$rounds=999999999$......salt......" */
574     if (strlen(out_buf) > 3 + 17 * rounds_custom + salt_len )
575         return NULL;
576
577     md = EVP_MD_CTX_new();
578     if (md == NULL
579         || !EVP_DigestInit_ex(md, sha, NULL)
580         || !EVP_DigestUpdate(md, passwd, passwd_len)
581         || !EVP_DigestUpdate(md, salt_copy, salt_len))
582         goto err;
583
584     md2 = EVP_MD_CTX_new();
585     if (md2 == NULL
586         || !EVP_DigestInit_ex(md2, sha, NULL)
587         || !EVP_DigestUpdate(md2, passwd, passwd_len)
588         || !EVP_DigestUpdate(md2, salt_copy, salt_len)
589         || !EVP_DigestUpdate(md2, passwd, passwd_len)
590         || !EVP_DigestFinal_ex(md2, buf, NULL))
591         goto err;
592
593     for (n = passwd_len; n > buf_size; n -= buf_size) {
594         if (!EVP_DigestUpdate(md, buf, buf_size))
595             goto err;
596     }
597     if (!EVP_DigestUpdate(md, buf, n))
598         goto err;
599
600     n = passwd_len;
601     while (n) {
602         if (!EVP_DigestUpdate(md,
603                               (n & 1) ? buf : (unsigned const char *)passwd,
604                               (n & 1) ? buf_size : passwd_len))
605             goto err;
606         n >>= 1;
607     }
608     if (!EVP_DigestFinal_ex(md, buf, NULL))
609         return NULL;
610
611     /* P sequence */
612     if (!EVP_DigestInit_ex(md2, sha, NULL))
613         goto err;
614
615     for (n = passwd_len; n > 0; n--)
616         if (!EVP_DigestUpdate(md2, passwd, passwd_len))
617             goto err;
618
619     if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
620         return NULL;
621
622     if ((p_bytes = OPENSSL_zalloc(passwd_len)) == NULL)
623         goto err;
624     for (cp = p_bytes, n = passwd_len; n > buf_size; n -= buf_size, cp += buf_size)
625         memcpy(cp, temp_buf, buf_size);
626     memcpy(cp, temp_buf, n);
627
628     /* S sequence */
629     if (!EVP_DigestInit_ex(md2, sha, NULL))
630         goto err;
631
632     for (n = 16 + buf[0]; n > 0; n--)
633         if (!EVP_DigestUpdate(md2, salt, salt_len))
634             goto err;
635
636     if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
637         return NULL;
638
639     if ((s_bytes = OPENSSL_zalloc(salt_len)) == NULL)
640         goto err;
641     for (cp = s_bytes, n = salt_len; n > buf_size; n -= buf_size, cp += buf_size)
642         memcpy(cp, temp_buf, buf_size);
643     memcpy(cp, temp_buf, n);
644
645     for (n = 0; n < rounds; n++) {
646         if (!EVP_DigestInit_ex(md2, sha, NULL))
647             goto err;
648         if (!EVP_DigestUpdate(md2,
649                               (n & 1) ? (unsigned const char *)p_bytes : buf,
650                               (n & 1) ? passwd_len : buf_size))
651             goto err;
652         if (n % 3) {
653             if (!EVP_DigestUpdate(md2, s_bytes, salt_len))
654                 goto err;
655         }
656         if (n % 7) {
657             if (!EVP_DigestUpdate(md2, p_bytes, passwd_len))
658                 goto err;
659         }
660         if (!EVP_DigestUpdate(md2,
661                               (n & 1) ? buf : (unsigned const char *)p_bytes,
662                               (n & 1) ? buf_size : passwd_len))
663                 goto err;
664         if (!EVP_DigestFinal_ex(md2, buf, NULL))
665                 goto err;
666     }
667     EVP_MD_CTX_free(md2);
668     EVP_MD_CTX_free(md);
669     md2 = NULL;
670     md = NULL;
671     OPENSSL_free(p_bytes);
672     OPENSSL_free(s_bytes);
673     p_bytes = NULL;
674     s_bytes = NULL;
675
676     cp = out_buf + strlen(out_buf);
677     *cp++ = '$';
678
679 #  define b64_from_24bit(B2, B1, B0, N)                                   \
680     do {                                                                \
681         unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0);             \
682         int i = (N);                                                    \
683         while (i-- > 0)                                                 \
684             {                                                           \
685                 *cp++ = cov_2char[w & 0x3f];                            \
686                 w >>= 6;                                                \
687             }                                                           \
688     } while (0)
689
690     switch (*magic) {
691     case '5':
692         b64_from_24bit (buf[0], buf[10], buf[20], 4);
693         b64_from_24bit (buf[21], buf[1], buf[11], 4);
694         b64_from_24bit (buf[12], buf[22], buf[2], 4);
695         b64_from_24bit (buf[3], buf[13], buf[23], 4);
696         b64_from_24bit (buf[24], buf[4], buf[14], 4);
697         b64_from_24bit (buf[15], buf[25], buf[5], 4);
698         b64_from_24bit (buf[6], buf[16], buf[26], 4);
699         b64_from_24bit (buf[27], buf[7], buf[17], 4);
700         b64_from_24bit (buf[18], buf[28], buf[8], 4);
701         b64_from_24bit (buf[9], buf[19], buf[29], 4);
702         b64_from_24bit (0, buf[31], buf[30], 3);
703         break;
704     case '6':
705         b64_from_24bit (buf[0], buf[21], buf[42], 4);
706         b64_from_24bit (buf[22], buf[43], buf[1], 4);
707         b64_from_24bit (buf[44], buf[2], buf[23], 4);
708         b64_from_24bit (buf[3], buf[24], buf[45], 4);
709         b64_from_24bit (buf[25], buf[46], buf[4], 4);
710         b64_from_24bit (buf[47], buf[5], buf[26], 4);
711         b64_from_24bit (buf[6], buf[27], buf[48], 4);
712         b64_from_24bit (buf[28], buf[49], buf[7], 4);
713         b64_from_24bit (buf[50], buf[8], buf[29], 4);
714         b64_from_24bit (buf[9], buf[30], buf[51], 4);
715         b64_from_24bit (buf[31], buf[52], buf[10], 4);
716         b64_from_24bit (buf[53], buf[11], buf[32], 4);
717         b64_from_24bit (buf[12], buf[33], buf[54], 4);
718         b64_from_24bit (buf[34], buf[55], buf[13], 4);
719         b64_from_24bit (buf[56], buf[14], buf[35], 4);
720         b64_from_24bit (buf[15], buf[36], buf[57], 4);
721         b64_from_24bit (buf[37], buf[58], buf[16], 4);
722         b64_from_24bit (buf[59], buf[17], buf[38], 4);
723         b64_from_24bit (buf[18], buf[39], buf[60], 4);
724         b64_from_24bit (buf[40], buf[61], buf[19], 4);
725         b64_from_24bit (buf[62], buf[20], buf[41], 4);
726         b64_from_24bit (0, 0, buf[63], 2);
727         break;
728     default:
729         goto err;
730     }
731     *cp = '\0';
732
733     return out_buf;
734
735  err:
736     EVP_MD_CTX_free(md2);
737     EVP_MD_CTX_free(md);
738     OPENSSL_free(p_bytes);
739     OPENSSL_free(s_bytes);
740     return NULL;
741 }
742 # endif
743
744 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
745                      char *passwd, BIO *out, int quiet, int table,
746                      int reverse, size_t pw_maxlen, passwd_modes mode)
747 {
748     char *hash = NULL;
749
750     assert(salt_p != NULL);
751     assert(salt_malloc_p != NULL);
752
753     /* first make sure we have a salt */
754     if (!passed_salt) {
755 # ifndef OPENSSL_NO_DES
756         if (mode == passwd_crypt) {
757             if (*salt_malloc_p == NULL)
758                 *salt_p = *salt_malloc_p = app_malloc(3, "salt buffer");
759             if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
760                 goto end;
761             (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
762             (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
763             (*salt_p)[2] = 0;
764 #  ifdef CHARSET_EBCDIC
765             ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert back
766                                                 * to ASCII */
767 #  endif
768         }
769 # endif                         /* !OPENSSL_NO_DES */
770
771 # ifndef NO_MD5CRYPT_1
772         if (mode == passwd_md5 || mode == passwd_apr1 || mode == passwd_aixmd5) {
773             int i;
774
775             if (*salt_malloc_p == NULL)
776                 *salt_p = *salt_malloc_p = app_malloc(9, "salt buffer");
777             if (RAND_bytes((unsigned char *)*salt_p, 8) <= 0)
778                 goto end;
779
780             for (i = 0; i < 8; i++)
781                 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
782             (*salt_p)[8] = 0;
783         }
784 # endif                         /* !NO_MD5CRYPT_1 */
785
786 # ifndef NO_SHACRYPT
787         if (mode == passwd_sha256 || mode == passwd_sha512) {
788             int i;
789
790             if (*salt_malloc_p == NULL)
791                 *salt_p = *salt_malloc_p = app_malloc(17, "salt buffer");
792             if (RAND_bytes((unsigned char *)*salt_p, 16) <= 0)
793                 goto end;
794
795             for (i = 0; i < 16; i++)
796                 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
797             (*salt_p)[16] = 0;
798         }
799 # endif                         /* !NO_SHACRYPT */
800     }
801
802     assert(*salt_p != NULL);
803
804     /* truncate password if necessary */
805     if ((strlen(passwd) > pw_maxlen)) {
806         if (!quiet)
807             /*
808              * XXX: really we should know how to print a size_t, not cast it
809              */
810             BIO_printf(bio_err,
811                        "Warning: truncating password to %u characters\n",
812                        (unsigned)pw_maxlen);
813         passwd[pw_maxlen] = 0;
814     }
815     assert(strlen(passwd) <= pw_maxlen);
816
817     /* now compute password hash */
818 # ifndef OPENSSL_NO_DES
819     if (mode == passwd_crypt)
820         hash = DES_crypt(passwd, *salt_p);
821 # endif
822 # ifndef NO_MD5CRYPT_1
823     if (mode == passwd_md5 || mode == passwd_apr1)
824         hash = md5crypt(passwd, (mode == passwd_md5 ? "1" : "apr1"), *salt_p);
825     if (mode == passwd_aixmd5)
826         hash = md5crypt(passwd, "", *salt_p);
827 # endif
828 # ifndef NO_SHACRYPT
829     if (mode == passwd_sha256 || mode == passwd_sha512)
830         hash = shacrypt(passwd, (mode == passwd_sha256 ? "5" : "6"), *salt_p);
831 # endif
832     assert(hash != NULL);
833
834     if (table && !reverse)
835         BIO_printf(out, "%s\t%s\n", passwd, hash);
836     else if (table && reverse)
837         BIO_printf(out, "%s\t%s\n", hash, passwd);
838     else
839         BIO_printf(out, "%s\n", hash);
840     return 1;
841
842  end:
843     return 0;
844 }
845 #else
846
847 int passwd_main(int argc, char **argv)
848 {
849     BIO_printf(bio_err, "Program not available.\n");
850     return (1);
851 }
852 #endif