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