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