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