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