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