]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/passwd.c
BSD-style MD5-based password algorithm in 'openssl passwd'.
[thirdparty/openssl.git] / apps / passwd.c
1 /* apps/passwd.c */
2
3 #if defined NO_MD5 || defined CHARSET_EBCDIC
4 # define NO_MD5CRYPT_1
5 #endif
6
7 #if !defined(NO_DES) || !defined(NO_MD5CRYPT_1)
8
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>
18
19 #ifndef NO_DES
20 # include <openssl/des.h>
21 #endif
22 #ifndef NO_MD5CRYPT_1
23 # include <openssl/md5.h>
24 #endif
25
26
27 #undef PROG
28 #define PROG passwd_main
29
30
31 static unsigned const char cov_2char[64]={
32 /* from crypto/des/fcrypt.c */
33 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
34 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
35 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
36 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
37 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
38 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
39 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
40 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
41 };
42
43 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
44 char *passwd, BIO *out, int quiet, int table, int reverse,
45 size_t pw_maxlen, int usecrypt, int use1, int useapr1);
46
47 /* -crypt - standard Unix password algorithm (default, only choice)
48 * -1 - MD5-based password algorithm
49 * -apr1 - MD5-based password algorithm, Apache variant
50 * -salt string - salt
51 * -in file - read passwords from file
52 * -stdin - read passwords from stdin
53 * -quiet - no warnings
54 * -table - format output as table
55 * -reverse - switch table columns
56 */
57
58 int MAIN(int, char **);
59
60 int MAIN(int argc, char **argv)
61 {
62 int ret = 1;
63 char *infile = NULL;
64 int in_stdin = 0;
65 char *salt = NULL, *passwd = NULL, **passwds = NULL;
66 char *salt_malloc = NULL, *passwd_malloc = NULL;
67 size_t passwd_malloc_size = 0;
68 int pw_source_defined = 0;
69 BIO *in = NULL, *out = NULL;
70 int i, badopt, opt_done;
71 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
72 int usecrypt = 0, use1 = 0, useapr1 = 0;
73 size_t pw_maxlen = 0;
74
75 apps_startup();
76
77 if (bio_err == NULL)
78 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
79 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
80 out = BIO_new(BIO_s_file());
81 if (out == NULL)
82 goto err;
83 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
84
85 badopt = 0, opt_done = 0;
86 i = 0;
87 while (!badopt && !opt_done && argv[++i] != NULL)
88 {
89 if (strcmp(argv[i], "-crypt") == 0)
90 usecrypt = 1;
91 else if (strcmp(argv[i], "-1") == 0)
92 use1 = 1;
93 else if (strcmp(argv[i], "-apr1") == 0)
94 useapr1 = 1;
95 else if (strcmp(argv[i], "-salt") == 0)
96 {
97 if ((argv[i+1] != NULL) && (salt == NULL))
98 {
99 passed_salt = 1;
100 salt = argv[++i];
101 }
102 else
103 badopt = 1;
104 }
105 else if (strcmp(argv[i], "-in") == 0)
106 {
107 if ((argv[i+1] != NULL) && !pw_source_defined)
108 {
109 pw_source_defined = 1;
110 infile = argv[++i];
111 }
112 else
113 badopt = 1;
114 }
115 else if (strcmp(argv[i], "-stdin") == 0)
116 {
117 if (!pw_source_defined)
118 {
119 pw_source_defined = 1;
120 in_stdin = 1;
121 }
122 else
123 badopt = 1;
124 }
125 else if (strcmp(argv[i], "-quiet") == 0)
126 quiet = 1;
127 else if (strcmp(argv[i], "-table") == 0)
128 table = 1;
129 else if (strcmp(argv[i], "-reverse") == 0)
130 reverse = 1;
131 else if (argv[i][0] == '-')
132 badopt = 1;
133 else if (!pw_source_defined)
134 /* non-option arguments, use as passwords */
135 {
136 pw_source_defined = 1;
137 passwds = &argv[i];
138 opt_done = 1;
139 }
140 else
141 badopt = 1;
142 }
143
144 if (!usecrypt && !use1 && !useapr1) /* use default */
145 usecrypt = 1;
146 if (usecrypt + use1 + useapr1 > 1) /* conflict */
147 badopt = 1;
148
149 /* reject unsupported algorithms */
150 #ifdef NO_DES
151 if (usecrypt) badopt = 1;
152 #endif
153 #ifdef NO_MD5CRYPT_1
154 if (use1 || useapr1) badopt = 1;
155 #endif
156
157 if (badopt)
158 {
159 BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
160 BIO_printf(bio_err, "where options are\n");
161 #ifndef NO_DES
162 BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n");
163 #endif
164 #ifndef NO_MD5CRYPT_1
165 BIO_printf(bio_err, "-1 MD5-based password algorithm\n");
166 BIO_printf(bio_err, "-apr1 MD5-based password algorithm, Apache variant\n");
167 #endif
168 BIO_printf(bio_err, "-salt string use provided salt\n");
169 BIO_printf(bio_err, "-in file read passwords from file\n");
170 BIO_printf(bio_err, "-stdin read passwords from stdin\n");
171 BIO_printf(bio_err, "-quiet no warnings\n");
172 BIO_printf(bio_err, "-table format output as table\n");
173 BIO_printf(bio_err, "-reverse switch table columns\n");
174
175 goto err;
176 }
177
178 if ((infile != NULL) || in_stdin)
179 {
180 in = BIO_new(BIO_s_file());
181 if (in == NULL)
182 goto err;
183 if (infile != NULL)
184 {
185 assert(in_stdin == 0);
186 if (BIO_read_filename(in, infile) <= 0)
187 goto err;
188 }
189 else
190 {
191 assert(in_stdin);
192 BIO_set_fp(in, stdin, BIO_NOCLOSE);
193 }
194 }
195
196 if (usecrypt)
197 pw_maxlen = 8;
198 else if (use1 || useapr1)
199 pw_maxlen = 256; /* arbitrary limit, should be enough for most passwords */
200
201 if (passwds == NULL)
202 {
203 /* no passwords on the command line */
204
205 passwd_malloc_size = pw_maxlen + 2;
206 /* longer than necessary so that we can warn about truncation */
207 passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
208 if (passwd_malloc == NULL)
209 goto err;
210 }
211
212 if ((in == NULL) && (passwds == NULL))
213 {
214 /* build a null-terminated list */
215 static char *passwds_static[2] = {NULL, NULL};
216
217 passwds = passwds_static;
218 if (in == NULL)
219 if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", 0) != 0)
220 goto err;
221 passwds[0] = passwd_malloc;
222 }
223
224 if (in == NULL)
225 {
226 assert(passwds != NULL);
227 assert(*passwds != NULL);
228
229 do /* loop over list of passwords */
230 {
231 passwd = *passwds++;
232 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
233 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
234 goto err;
235 }
236 while (*passwds != NULL);
237 }
238 else
239 /* in != NULL */
240 {
241 int done;
242
243 assert (passwd != NULL);
244 do
245 {
246 int r = BIO_gets(in, passwd, pw_maxlen + 1);
247 if (r > 0)
248 {
249 char *c = (strchr(passwd, '\n')) ;
250 if (c != NULL)
251 *c = 0; /* truncate at newline */
252 else
253 {
254 /* ignore rest of line */
255 char trash[BUFSIZ];
256 do
257 r = BIO_gets(in, trash, sizeof trash);
258 while ((r > 0) && (!strchr(trash, '\n')));
259 }
260
261 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
262 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
263 goto err;
264 }
265 done = (r <= 0);
266 }
267 while (!done);
268 }
269
270 err:
271 ERR_print_errors(bio_err);
272 if (salt_malloc)
273 OPENSSL_free(salt_malloc);
274 if (passwd_malloc)
275 OPENSSL_free(passwd_malloc);
276 if (in)
277 BIO_free(in);
278 if (out)
279 BIO_free(out);
280 EXIT(ret);
281 }
282
283
284 #ifndef NO_MD5CRYPT_1
285 /* MD5-based password algorithm (should probably be available as a library
286 * function; then the static buffer would not be acceptable).
287 * For magic string "1", this should be compatible to the MD5-based BSD
288 * password algorithm.
289 * For 'magic' string "apr1", this is compatible to the MD5-based Apache
290 * password algorithm.
291 * (Apparently, the Apache password algorithm is identical except that the
292 * 'magic' string was changed -- the laziest application of the NIH principle
293 * I've ever encountered.)
294 */
295 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
296 {
297 static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
298 unsigned char buf[MD5_DIGEST_LENGTH];
299 char *salt_out;
300 int n, i;
301 MD5_CTX md;
302 size_t passwd_len, salt_len;
303
304 passwd_len = strlen(passwd);
305 out_buf[0] = '$';
306 out_buf[1] = 0;
307 assert(strlen(magic) <= 4); /* "1" or "apr1" */
308 strncat(out_buf, magic, 4);
309 strncat(out_buf, "$", 1);
310 strncat(out_buf, salt, 8);
311 assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
312 salt_out = out_buf + 6;
313 salt_len = strlen(salt_out);
314 assert(salt_len <= 8);
315
316 MD5_Init(&md);
317 MD5_Update(&md, passwd, passwd_len);
318 MD5_Update(&md, "$", 1);
319 MD5_Update(&md, magic, strlen(magic));
320 MD5_Update(&md, "$", 1);
321 MD5_Update(&md, salt_out, salt_len);
322
323 {
324 MD5_CTX md2;
325
326 MD5_Init(&md2);
327 MD5_Update(&md2, passwd, passwd_len);
328 MD5_Update(&md2, salt_out, salt_len);
329 MD5_Update(&md2, passwd, passwd_len);
330 MD5_Final(buf, &md2);
331 }
332 for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
333 MD5_Update(&md, buf, sizeof buf);
334 MD5_Update(&md, buf, i);
335
336 n = passwd_len;
337 while (n)
338 {
339 MD5_Update(&md, (n & 1) ? "\0" : passwd, 1);
340 n >>= 1;
341 }
342 MD5_Final(buf, &md);
343
344 for (i = 0; i < 1000; i++)
345 {
346 MD5_CTX md2;
347
348 MD5_Init(&md2);
349 MD5_Update(&md2, (i & 1) ? (unsigned char *) passwd : buf,
350 (i & 1) ? passwd_len : sizeof buf);
351 if (i % 3)
352 MD5_Update(&md2, salt_out, salt_len);
353 if (i % 7)
354 MD5_Update(&md2, passwd, passwd_len);
355 MD5_Update(&md2, (i & 1) ? buf : (unsigned char *) passwd,
356 (i & 1) ? sizeof buf : passwd_len);
357 MD5_Final(buf, &md2);
358 }
359
360 {
361 /* transform buf into output string */
362
363 unsigned char buf_perm[sizeof buf];
364 int dest, source;
365 char *output;
366
367 /* silly output permutation */
368 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
369 buf_perm[dest] = buf[source];
370 buf_perm[14] = buf[5];
371 buf_perm[15] = buf[11];
372 #ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
373 assert(16 == sizeof buf_perm);
374 #endif
375
376 output = salt_out + salt_len;
377 assert(output == out_buf + strlen(out_buf));
378
379 *output++ = '$';
380
381 for (i = 0; i < 15; i += 3)
382 {
383 *output++ = cov_2char[buf_perm[i+2] & 0x3f];
384 *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
385 (buf_perm[i+2] >> 6)];
386 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
387 (buf_perm[i+1] >> 4)];
388 *output++ = cov_2char[buf_perm[i] >> 2];
389 }
390 assert(i == 15);
391 *output++ = cov_2char[buf_perm[i] & 0x3f];
392 *output++ = cov_2char[buf_perm[i] >> 6];
393 *output = 0;
394 assert(strlen(out_buf) < sizeof(out_buf));
395 }
396
397 return out_buf;
398 }
399 #endif
400
401
402 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
403 char *passwd, BIO *out, int quiet, int table, int reverse,
404 size_t pw_maxlen, int usecrypt, int use1, int useapr1)
405 {
406 char *hash = NULL;
407
408 assert(salt_p != NULL);
409 assert(salt_malloc_p != NULL);
410
411 /* first make sure we have a salt */
412 if (!passed_salt)
413 {
414 #ifndef NO_DES
415 if (usecrypt)
416 {
417 if (*salt_malloc_p == NULL)
418 {
419 *salt_p = *salt_malloc_p = OPENSSL_malloc(3);
420 if (*salt_malloc_p == NULL)
421 goto err;
422 }
423 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
424 goto err;
425 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
426 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
427 (*salt_p)[2] = 0;
428 #ifdef CHARSET_EBCDIC
429 ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
430 * back to ASCII */
431 #endif
432 }
433 #endif /* !NO_DES */
434
435 #ifndef NO_MD5CRYPT_1
436 if (use1 || useapr1)
437 {
438 int i;
439
440 if (*salt_malloc_p == NULL)
441 {
442 *salt_p = *salt_malloc_p = OPENSSL_malloc(9);
443 if (*salt_malloc_p == NULL)
444 goto err;
445 }
446 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
447 goto err;
448
449 for (i = 0; i < 8; i++)
450 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
451 (*salt_p)[8] = 0;
452 }
453 #endif /* !NO_MD5CRYPT_1 */
454 }
455
456 assert(*salt_p != NULL);
457
458 /* truncate password if necessary */
459 if ((strlen(passwd) > pw_maxlen))
460 {
461 if (!quiet)
462 BIO_printf(bio_err, "Warning: truncating password to %u characters\n", pw_maxlen);
463 passwd[pw_maxlen] = 0;
464 }
465 assert(strlen(passwd) <= pw_maxlen);
466
467 /* now compute password hash */
468 #ifndef NO_DES
469 if (usecrypt)
470 hash = des_crypt(passwd, *salt_p);
471 #endif
472 #ifndef NO_MD5CRYPT_1
473 if (use1 || useapr1)
474 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
475 #endif
476 assert(hash != NULL);
477
478 if (table && !reverse)
479 BIO_printf(out, "%s\t%s\n", passwd, hash);
480 else if (table && reverse)
481 BIO_printf(out, "%s\t%s\n", hash, passwd);
482 else
483 BIO_printf(out, "%s\n", hash);
484 return 1;
485
486 err:
487 return 0;
488 }
489 #else
490
491 int MAIN(int argc, char **argv)
492 {
493 fputs("Program not available.\n", stderr)
494 EXIT(1);
495 }
496 #endif