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