blob: 9ca25dd1da811fe44934e5e7cce3d4208973cadf [file] [log] [blame]
Bodo Möllerbb325c72000-02-10 21:50:52 +00001/* apps/passwd.c */
2
Richard Levittecf1b7d92001-02-19 16:06:34 +00003#if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
Bodo Möller1f4643a2000-06-23 18:00:16 +00004# define NO_MD5CRYPT_1
Bodo Möller3ebf0be2000-02-11 17:18:50 +00005#endif
6
Richard Levittecf1b7d92001-02-19 16:06:34 +00007#if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
Bodo Möllerbb325c72000-02-10 21:50:52 +00008
9#include <assert.h>
10#include <string.h>
11
12#include "apps.h"
13
14#include <openssl/bio.h>
15#include <openssl/err.h>
16#include <openssl/evp.h>
17#include <openssl/rand.h>
Richard Levittecf1b7d92001-02-19 16:06:34 +000018#ifndef OPENSSL_NO_DES
Richard Levitte125cc352002-03-22 02:42:57 +000019# include <openssl/des.h>
Bodo Möllerbb325c72000-02-10 21:50:52 +000020#endif
Bodo Möller1f4643a2000-06-23 18:00:16 +000021#ifndef NO_MD5CRYPT_1
Ben Lauriedbad1692001-07-30 23:57:25 +000022# include <openssl/md5.h>
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000023#endif
24
Bodo Möllerbb325c72000-02-10 21:50:52 +000025
26#undef PROG
27#define PROG passwd_main
28
29
30static unsigned const char cov_2char[64]={
31 /* from crypto/des/fcrypt.c */
32 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
33 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
34 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
35 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
36 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
37 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
38 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
39 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
40};
41
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000042static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
43 char *passwd, BIO *out, int quiet, int table, int reverse,
Bodo Möller1f4643a2000-06-23 18:00:16 +000044 size_t pw_maxlen, int usecrypt, int use1, int useapr1);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000045
Bodo Möllercc244b32000-07-31 12:27:44 +000046/* -crypt - standard Unix password algorithm (default)
Bodo Möller1f4643a2000-06-23 18:00:16 +000047 * -1 - MD5-based password algorithm
48 * -apr1 - MD5-based password algorithm, Apache variant
Bodo Möllerbb325c72000-02-10 21:50:52 +000049 * -salt string - salt
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000050 * -in file - read passwords from file
51 * -stdin - read passwords from stdin
Bodo Möllerdb70a3f2000-11-17 09:03:02 +000052 * -noverify - never verify when reading password from terminal
Bodo Möllerbb325c72000-02-10 21:50:52 +000053 * -quiet - no warnings
54 * -table - format output as table
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000055 * -reverse - switch table columns
Bodo Möllerbb325c72000-02-10 21:50:52 +000056 */
57
Ralf S. Engelschall07fb39c2000-02-24 10:37:58 +000058int MAIN(int, char **);
59
Bodo Möllerbb325c72000-02-10 21:50:52 +000060int MAIN(int argc, char **argv)
61 {
62 int ret = 1;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000063 char *infile = NULL;
64 int in_stdin = 0;
Bodo Möllerdb70a3f2000-11-17 09:03:02 +000065 int in_noverify = 0;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000066 char *salt = NULL, *passwd = NULL, **passwds = NULL;
Bodo Möllerbb325c72000-02-10 21:50:52 +000067 char *salt_malloc = NULL, *passwd_malloc = NULL;
Bodo Möller7fc840c2000-04-27 09:11:28 +000068 size_t passwd_malloc_size = 0;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000069 int pw_source_defined = 0;
70 BIO *in = NULL, *out = NULL;
Bodo Möllerbb325c72000-02-10 21:50:52 +000071 int i, badopt, opt_done;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +000072 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
Bodo Möller1f4643a2000-06-23 18:00:16 +000073 int usecrypt = 0, use1 = 0, useapr1 = 0;
Bodo Möllerbb325c72000-02-10 21:50:52 +000074 size_t pw_maxlen = 0;
75
76 apps_startup();
77
78 if (bio_err == NULL)
79 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
80 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
Dr. Stephen Henson3647bee2002-02-22 14:01:21 +000081
82 if (!load_config(bio_err, NULL))
83 goto err;
Bodo Möllerbb325c72000-02-10 21:50:52 +000084 out = BIO_new(BIO_s_file());
85 if (out == NULL)
86 goto err;
87 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
Richard Levittebc36ee62001-02-20 08:13:47 +000088#ifdef OPENSSL_SYS_VMS
Richard Levitte645749e2000-09-20 13:55:50 +000089 {
90 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
91 out = BIO_push(tmpbio, out);
92 }
93#endif
Bodo Möllerbb325c72000-02-10 21:50:52 +000094
95 badopt = 0, opt_done = 0;
96 i = 0;
97 while (!badopt && !opt_done && argv[++i] != NULL)
98 {
99 if (strcmp(argv[i], "-crypt") == 0)
Ben Laurieefb41622000-02-11 13:11:18 +0000100 usecrypt = 1;
Bodo Möller1f4643a2000-06-23 18:00:16 +0000101 else if (strcmp(argv[i], "-1") == 0)
102 use1 = 1;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000103 else if (strcmp(argv[i], "-apr1") == 0)
104 useapr1 = 1;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000105 else if (strcmp(argv[i], "-salt") == 0)
106 {
107 if ((argv[i+1] != NULL) && (salt == NULL))
108 {
109 passed_salt = 1;
110 salt = argv[++i];
111 }
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000112 else
113 badopt = 1;
114 }
115 else if (strcmp(argv[i], "-in") == 0)
116 {
117 if ((argv[i+1] != NULL) && !pw_source_defined)
118 {
119 pw_source_defined = 1;
120 infile = argv[++i];
121 }
122 else
123 badopt = 1;
124 }
125 else if (strcmp(argv[i], "-stdin") == 0)
126 {
127 if (!pw_source_defined)
128 {
129 pw_source_defined = 1;
130 in_stdin = 1;
131 }
132 else
133 badopt = 1;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000134 }
Bodo Möllerdb70a3f2000-11-17 09:03:02 +0000135 else if (strcmp(argv[i], "-noverify") == 0)
136 in_noverify = 1;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000137 else if (strcmp(argv[i], "-quiet") == 0)
138 quiet = 1;
139 else if (strcmp(argv[i], "-table") == 0)
140 table = 1;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000141 else if (strcmp(argv[i], "-reverse") == 0)
142 reverse = 1;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000143 else if (argv[i][0] == '-')
144 badopt = 1;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000145 else if (!pw_source_defined)
146 /* non-option arguments, use as passwords */
Bodo Möllerbb325c72000-02-10 21:50:52 +0000147 {
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000148 pw_source_defined = 1;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000149 passwds = &argv[i];
150 opt_done = 1;
151 }
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000152 else
153 badopt = 1;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000154 }
155
Bodo Möller1f4643a2000-06-23 18:00:16 +0000156 if (!usecrypt && !use1 && !useapr1) /* use default */
Ben Laurieefb41622000-02-11 13:11:18 +0000157 usecrypt = 1;
Bodo Möller1f4643a2000-06-23 18:00:16 +0000158 if (usecrypt + use1 + useapr1 > 1) /* conflict */
Bodo Möllerbb325c72000-02-10 21:50:52 +0000159 badopt = 1;
160
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000161 /* reject unsupported algorithms */
Richard Levittecf1b7d92001-02-19 16:06:34 +0000162#ifdef OPENSSL_NO_DES
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000163 if (usecrypt) badopt = 1;
164#endif
Bodo Möller1f4643a2000-06-23 18:00:16 +0000165#ifdef NO_MD5CRYPT_1
166 if (use1 || useapr1) badopt = 1;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000167#endif
168
Bodo Möllerbb325c72000-02-10 21:50:52 +0000169 if (badopt)
170 {
171 BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
172 BIO_printf(bio_err, "where options are\n");
Richard Levittecf1b7d92001-02-19 16:06:34 +0000173#ifndef OPENSSL_NO_DES
Bodo Möllerbb325c72000-02-10 21:50:52 +0000174 BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n");
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000175#endif
Bodo Möller1f4643a2000-06-23 18:00:16 +0000176#ifndef NO_MD5CRYPT_1
177 BIO_printf(bio_err, "-1 MD5-based password algorithm\n");
178 BIO_printf(bio_err, "-apr1 MD5-based password algorithm, Apache variant\n");
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000179#endif
Bodo Möllerbb325c72000-02-10 21:50:52 +0000180 BIO_printf(bio_err, "-salt string use provided salt\n");
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000181 BIO_printf(bio_err, "-in file read passwords from file\n");
182 BIO_printf(bio_err, "-stdin read passwords from stdin\n");
Bodo Möllerdb70a3f2000-11-17 09:03:02 +0000183 BIO_printf(bio_err, "-noverify never verify when reading password from terminal\n");
Bodo Möllerbb325c72000-02-10 21:50:52 +0000184 BIO_printf(bio_err, "-quiet no warnings\n");
185 BIO_printf(bio_err, "-table format output as table\n");
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000186 BIO_printf(bio_err, "-reverse switch table columns\n");
Bodo Möllerbb325c72000-02-10 21:50:52 +0000187
188 goto err;
189 }
190
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000191 if ((infile != NULL) || in_stdin)
192 {
193 in = BIO_new(BIO_s_file());
194 if (in == NULL)
195 goto err;
196 if (infile != NULL)
197 {
198 assert(in_stdin == 0);
199 if (BIO_read_filename(in, infile) <= 0)
200 goto err;
201 }
202 else
203 {
204 assert(in_stdin);
205 BIO_set_fp(in, stdin, BIO_NOCLOSE);
206 }
207 }
208
Ben Laurieefb41622000-02-11 13:11:18 +0000209 if (usecrypt)
Bodo Möllerbb325c72000-02-10 21:50:52 +0000210 pw_maxlen = 8;
Bodo Möller1f4643a2000-06-23 18:00:16 +0000211 else if (use1 || useapr1)
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000212 pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */
Bodo Möllerbb325c72000-02-10 21:50:52 +0000213
214 if (passwds == NULL)
215 {
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000216 /* no passwords on the command line */
Bodo Möller7fc840c2000-04-27 09:11:28 +0000217
218 passwd_malloc_size = pw_maxlen + 2;
Bodo Möller4adcfa02000-04-27 06:47:23 +0000219 /* longer than necessary so that we can warn about truncation */
Richard Levitte26a3a482000-06-01 22:19:21 +0000220 passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
Bodo Möllerbb325c72000-02-10 21:50:52 +0000221 if (passwd_malloc == NULL)
222 goto err;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000223 }
224
225 if ((in == NULL) && (passwds == NULL))
226 {
227 /* build a null-terminated list */
228 static char *passwds_static[2] = {NULL, NULL};
229
230 passwds = passwds_static;
231 if (in == NULL)
Bodo Möllerdb70a3f2000-11-17 09:03:02 +0000232 if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", !(passed_salt || in_noverify)) != 0)
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000233 goto err;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000234 passwds[0] = passwd_malloc;
235 }
236
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000237 if (in == NULL)
Bodo Möllerbb325c72000-02-10 21:50:52 +0000238 {
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000239 assert(passwds != NULL);
240 assert(*passwds != NULL);
Bodo Möllerbb325c72000-02-10 21:50:52 +0000241
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000242 do /* loop over list of passwords */
Bodo Möllerbb325c72000-02-10 21:50:52 +0000243 {
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000244 passwd = *passwds++;
245 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
Bodo Möller1f4643a2000-06-23 18:00:16 +0000246 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000247 goto err;
Bodo Möllerbb325c72000-02-10 21:50:52 +0000248 }
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000249 while (*passwds != NULL);
Bodo Möllerbb325c72000-02-10 21:50:52 +0000250 }
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000251 else
252 /* in != NULL */
253 {
254 int done;
255
256 assert (passwd != NULL);
257 do
258 {
259 int r = BIO_gets(in, passwd, pw_maxlen + 1);
260 if (r > 0)
261 {
262 char *c = (strchr(passwd, '\n')) ;
263 if (c != NULL)
264 *c = 0; /* truncate at newline */
265 else
266 {
267 /* ignore rest of line */
268 char trash[BUFSIZ];
269 do
270 r = BIO_gets(in, trash, sizeof trash);
271 while ((r > 0) && (!strchr(trash, '\n')));
272 }
273
274 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
Bodo Möller1f4643a2000-06-23 18:00:16 +0000275 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000276 goto err;
277 }
278 done = (r <= 0);
279 }
280 while (!done);
281 }
Bodo Möllercad4b842001-03-13 22:17:10 +0000282 ret = 0;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000283
Bodo Möllerbb325c72000-02-10 21:50:52 +0000284err:
285 ERR_print_errors(bio_err);
286 if (salt_malloc)
Richard Levitte26a3a482000-06-01 22:19:21 +0000287 OPENSSL_free(salt_malloc);
Bodo Möllerbb325c72000-02-10 21:50:52 +0000288 if (passwd_malloc)
Richard Levitte26a3a482000-06-01 22:19:21 +0000289 OPENSSL_free(passwd_malloc);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000290 if (in)
291 BIO_free(in);
Bodo Möllerbb325c72000-02-10 21:50:52 +0000292 if (out)
Richard Levitte645749e2000-09-20 13:55:50 +0000293 BIO_free_all(out);
Richard Levittec04f8cf2001-06-23 16:37:32 +0000294 apps_shutdown();
Richard Levitte1c3e4a32002-12-03 16:33:03 +0000295 OPENSSL_EXIT(ret);
Bodo Möllerbb325c72000-02-10 21:50:52 +0000296 }
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000297
298
Bodo Möller1f4643a2000-06-23 18:00:16 +0000299#ifndef NO_MD5CRYPT_1
300/* MD5-based password algorithm (should probably be available as a library
301 * function; then the static buffer would not be acceptable).
302 * For magic string "1", this should be compatible to the MD5-based BSD
303 * password algorithm.
304 * For 'magic' string "apr1", this is compatible to the MD5-based Apache
305 * password algorithm.
306 * (Apparently, the Apache password algorithm is identical except that the
307 * 'magic' string was changed -- the laziest application of the NIH principle
308 * I've ever encountered.)
309 */
310static char *md5crypt(const char *passwd, const char *magic, const char *salt)
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000311 {
312 static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
313 unsigned char buf[MD5_DIGEST_LENGTH];
314 char *salt_out;
Geoff Thorpe27545972003-10-29 20:24:15 +0000315 int n;
316 unsigned int i;
Ben Lauriedbad1692001-07-30 23:57:25 +0000317 EVP_MD_CTX md,md2;
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000318 size_t passwd_len, salt_len;
319
320 passwd_len = strlen(passwd);
Bodo Möller1f4643a2000-06-23 18:00:16 +0000321 out_buf[0] = '$';
322 out_buf[1] = 0;
323 assert(strlen(magic) <= 4); /* "1" or "apr1" */
324 strncat(out_buf, magic, 4);
325 strncat(out_buf, "$", 1);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000326 strncat(out_buf, salt, 8);
327 assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
Bodo Möller57108f02001-01-19 07:37:56 +0000328 salt_out = out_buf + 2 + strlen(magic);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000329 salt_len = strlen(salt_out);
330 assert(salt_len <= 8);
331
Ben Lauriedbad1692001-07-30 23:57:25 +0000332 EVP_MD_CTX_init(&md);
Dr. Stephen Henson20d21862001-10-16 01:24:29 +0000333 EVP_DigestInit_ex(&md,EVP_md5(), NULL);
Dr. Stephen Henson323f2892001-06-19 22:30:40 +0000334 EVP_DigestUpdate(&md, passwd, passwd_len);
335 EVP_DigestUpdate(&md, "$", 1);
336 EVP_DigestUpdate(&md, magic, strlen(magic));
337 EVP_DigestUpdate(&md, "$", 1);
338 EVP_DigestUpdate(&md, salt_out, salt_len);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000339
Ben Lauriedbad1692001-07-30 23:57:25 +0000340 EVP_MD_CTX_init(&md2);
Dr. Stephen Henson20d21862001-10-16 01:24:29 +0000341 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
Ben Lauriedbad1692001-07-30 23:57:25 +0000342 EVP_DigestUpdate(&md2, passwd, passwd_len);
343 EVP_DigestUpdate(&md2, salt_out, salt_len);
344 EVP_DigestUpdate(&md2, passwd, passwd_len);
Dr. Stephen Henson20d21862001-10-16 01:24:29 +0000345 EVP_DigestFinal_ex(&md2, buf, NULL);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000346
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000347 for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
Dr. Stephen Henson323f2892001-06-19 22:30:40 +0000348 EVP_DigestUpdate(&md, buf, sizeof buf);
349 EVP_DigestUpdate(&md, buf, i);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000350
351 n = passwd_len;
352 while (n)
353 {
Dr. Stephen Henson323f2892001-06-19 22:30:40 +0000354 EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000355 n >>= 1;
356 }
Dr. Stephen Henson20d21862001-10-16 01:24:29 +0000357 EVP_DigestFinal_ex(&md, buf, NULL);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000358
359 for (i = 0; i < 1000; i++)
360 {
Dr. Stephen Henson20d21862001-10-16 01:24:29 +0000361 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
Nils Larsch7d727232005-04-05 19:11:19 +0000362 EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf,
Dr. Stephen Henson323f2892001-06-19 22:30:40 +0000363 (i & 1) ? passwd_len : sizeof buf);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000364 if (i % 3)
Dr. Stephen Henson323f2892001-06-19 22:30:40 +0000365 EVP_DigestUpdate(&md2, salt_out, salt_len);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000366 if (i % 7)
Dr. Stephen Henson323f2892001-06-19 22:30:40 +0000367 EVP_DigestUpdate(&md2, passwd, passwd_len);
Nils Larsch7d727232005-04-05 19:11:19 +0000368 EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd,
Dr. Stephen Henson323f2892001-06-19 22:30:40 +0000369 (i & 1) ? sizeof buf : passwd_len);
Dr. Stephen Henson20d21862001-10-16 01:24:29 +0000370 EVP_DigestFinal_ex(&md2, buf, NULL);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000371 }
Ben Lauriedbad1692001-07-30 23:57:25 +0000372 EVP_MD_CTX_cleanup(&md2);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000373
374 {
375 /* transform buf into output string */
376
377 unsigned char buf_perm[sizeof buf];
378 int dest, source;
379 char *output;
380
381 /* silly output permutation */
382 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
383 buf_perm[dest] = buf[source];
384 buf_perm[14] = buf[5];
385 buf_perm[15] = buf[11];
Ben Lauriebd445702000-02-16 12:09:17 +0000386#ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000387 assert(16 == sizeof buf_perm);
Ben Lauriebd445702000-02-16 12:09:17 +0000388#endif
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000389
390 output = salt_out + salt_len;
391 assert(output == out_buf + strlen(out_buf));
392
393 *output++ = '$';
394
395 for (i = 0; i < 15; i += 3)
396 {
397 *output++ = cov_2char[buf_perm[i+2] & 0x3f];
398 *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
399 (buf_perm[i+2] >> 6)];
400 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
401 (buf_perm[i+1] >> 4)];
402 *output++ = cov_2char[buf_perm[i] >> 2];
403 }
404 assert(i == 15);
405 *output++ = cov_2char[buf_perm[i] & 0x3f];
406 *output++ = cov_2char[buf_perm[i] >> 6];
407 *output = 0;
408 assert(strlen(out_buf) < sizeof(out_buf));
409 }
Ben Lauriedbad1692001-07-30 23:57:25 +0000410 EVP_MD_CTX_cleanup(&md);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000411
412 return out_buf;
413 }
Bodo Möllerbb325c72000-02-10 21:50:52 +0000414#endif
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000415
416
417static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
418 char *passwd, BIO *out, int quiet, int table, int reverse,
Bodo Möller1f4643a2000-06-23 18:00:16 +0000419 size_t pw_maxlen, int usecrypt, int use1, int useapr1)
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000420 {
421 char *hash = NULL;
422
423 assert(salt_p != NULL);
424 assert(salt_malloc_p != NULL);
425
426 /* first make sure we have a salt */
427 if (!passed_salt)
428 {
Richard Levittecf1b7d92001-02-19 16:06:34 +0000429#ifndef OPENSSL_NO_DES
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000430 if (usecrypt)
431 {
432 if (*salt_malloc_p == NULL)
433 {
Richard Levitte26a3a482000-06-01 22:19:21 +0000434 *salt_p = *salt_malloc_p = OPENSSL_malloc(3);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000435 if (*salt_malloc_p == NULL)
436 goto err;
437 }
438 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
439 goto err;
440 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
441 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
442 (*salt_p)[2] = 0;
443#ifdef CHARSET_EBCDIC
444 ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
445 * back to ASCII */
446#endif
447 }
Richard Levittecf1b7d92001-02-19 16:06:34 +0000448#endif /* !OPENSSL_NO_DES */
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000449
Bodo Möller1f4643a2000-06-23 18:00:16 +0000450#ifndef NO_MD5CRYPT_1
451 if (use1 || useapr1)
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000452 {
453 int i;
454
455 if (*salt_malloc_p == NULL)
456 {
Richard Levitte26a3a482000-06-01 22:19:21 +0000457 *salt_p = *salt_malloc_p = OPENSSL_malloc(9);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000458 if (*salt_malloc_p == NULL)
459 goto err;
460 }
461 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
462 goto err;
463
464 for (i = 0; i < 8; i++)
465 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
466 (*salt_p)[8] = 0;
467 }
Bodo Möller1f4643a2000-06-23 18:00:16 +0000468#endif /* !NO_MD5CRYPT_1 */
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000469 }
470
471 assert(*salt_p != NULL);
472
473 /* truncate password if necessary */
474 if ((strlen(passwd) > pw_maxlen))
475 {
476 if (!quiet)
Ben Lauriea51a9722005-06-29 11:02:15 +0000477 /* XXX: really we should know how to print a size_t, not cast it */
478 BIO_printf(bio_err, "Warning: truncating password to %u characters\n", (unsigned)pw_maxlen);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000479 passwd[pw_maxlen] = 0;
480 }
481 assert(strlen(passwd) <= pw_maxlen);
482
483 /* now compute password hash */
Richard Levittecf1b7d92001-02-19 16:06:34 +0000484#ifndef OPENSSL_NO_DES
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000485 if (usecrypt)
Richard Levitte125cc352002-03-22 02:42:57 +0000486 hash = DES_crypt(passwd, *salt_p);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000487#endif
Bodo Möller1f4643a2000-06-23 18:00:16 +0000488#ifndef NO_MD5CRYPT_1
489 if (use1 || useapr1)
490 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000491#endif
492 assert(hash != NULL);
493
494 if (table && !reverse)
495 BIO_printf(out, "%s\t%s\n", passwd, hash);
496 else if (table && reverse)
497 BIO_printf(out, "%s\t%s\n", hash, passwd);
498 else
499 BIO_printf(out, "%s\n", hash);
500 return 1;
501
502err:
503 return 0;
504 }
Bodo Möller3ebf0be2000-02-11 17:18:50 +0000505#else
506
507int MAIN(int argc, char **argv)
508 {
509 fputs("Program not available.\n", stderr)
Richard Levitte1c3e4a32002-12-03 16:33:03 +0000510 OPENSSL_EXIT(1);
Bodo Möller3ebf0be2000-02-11 17:18:50 +0000511 }
Bodo Möllere6e7b5f2000-02-11 16:25:44 +0000512#endif