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