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