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