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