00001
00002
00003 #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
00004 # define NO_MD5CRYPT_1
00005 #endif
00006
00007 #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1)
00008
00009 #include <assert.h>
00010 #include <string.h>
00011
00012 #include "apps.h"
00013
00014 #include <openssl/bio.h>
00015 #include <openssl/err.h>
00016 #include <openssl/evp.h>
00017 #include <openssl/rand.h>
00018 #ifndef OPENSSL_NO_DES
00019 # include <openssl/des.h>
00020 #endif
00021 #ifndef NO_MD5CRYPT_1
00022 # include <openssl/md5.h>
00023 #endif
00024
00025
00026 #undef PROG
00027 #define PROG passwd_main
00028
00029
00030 static unsigned const char cov_2char[64]={
00031
00032 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
00033 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
00034 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
00035 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
00036 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
00037 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
00038 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
00039 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
00040 };
00041
00042 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
00043 char *passwd, BIO *out, int quiet, int table, int reverse,
00044 size_t pw_maxlen, int usecrypt, int use1, int useapr1);
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 int MAIN(int, char **);
00059
00060 int MAIN(int argc, char **argv)
00061 {
00062 int ret = 1;
00063 char *infile = NULL;
00064 int in_stdin = 0;
00065 int in_noverify = 0;
00066 char *salt = NULL, *passwd = NULL, **passwds = NULL;
00067 char *salt_malloc = NULL, *passwd_malloc = NULL;
00068 size_t passwd_malloc_size = 0;
00069 int pw_source_defined = 0;
00070 BIO *in = NULL, *out = NULL;
00071 int i, badopt, opt_done;
00072 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
00073 int usecrypt = 0, use1 = 0, useapr1 = 0;
00074 size_t pw_maxlen = 0;
00075
00076 apps_startup();
00077
00078 if (bio_err == NULL)
00079 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
00080 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
00081
00082 if (!load_config(bio_err, NULL))
00083 goto err;
00084 out = BIO_new(BIO_s_file());
00085 if (out == NULL)
00086 goto err;
00087 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
00088 #ifdef OPENSSL_SYS_VMS
00089 {
00090 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00091 out = BIO_push(tmpbio, out);
00092 }
00093 #endif
00094
00095 badopt = 0, opt_done = 0;
00096 i = 0;
00097 while (!badopt && !opt_done && argv[++i] != NULL)
00098 {
00099 if (strcmp(argv[i], "-crypt") == 0)
00100 usecrypt = 1;
00101 else if (strcmp(argv[i], "-1") == 0)
00102 use1 = 1;
00103 else if (strcmp(argv[i], "-apr1") == 0)
00104 useapr1 = 1;
00105 else if (strcmp(argv[i], "-salt") == 0)
00106 {
00107 if ((argv[i+1] != NULL) && (salt == NULL))
00108 {
00109 passed_salt = 1;
00110 salt = argv[++i];
00111 }
00112 else
00113 badopt = 1;
00114 }
00115 else if (strcmp(argv[i], "-in") == 0)
00116 {
00117 if ((argv[i+1] != NULL) && !pw_source_defined)
00118 {
00119 pw_source_defined = 1;
00120 infile = argv[++i];
00121 }
00122 else
00123 badopt = 1;
00124 }
00125 else if (strcmp(argv[i], "-stdin") == 0)
00126 {
00127 if (!pw_source_defined)
00128 {
00129 pw_source_defined = 1;
00130 in_stdin = 1;
00131 }
00132 else
00133 badopt = 1;
00134 }
00135 else if (strcmp(argv[i], "-noverify") == 0)
00136 in_noverify = 1;
00137 else if (strcmp(argv[i], "-quiet") == 0)
00138 quiet = 1;
00139 else if (strcmp(argv[i], "-table") == 0)
00140 table = 1;
00141 else if (strcmp(argv[i], "-reverse") == 0)
00142 reverse = 1;
00143 else if (argv[i][0] == '-')
00144 badopt = 1;
00145 else if (!pw_source_defined)
00146
00147 {
00148 pw_source_defined = 1;
00149 passwds = &argv[i];
00150 opt_done = 1;
00151 }
00152 else
00153 badopt = 1;
00154 }
00155
00156 if (!usecrypt && !use1 && !useapr1)
00157 usecrypt = 1;
00158 if (usecrypt + use1 + useapr1 > 1)
00159 badopt = 1;
00160
00161
00162 #ifdef OPENSSL_NO_DES
00163 if (usecrypt) badopt = 1;
00164 #endif
00165 #ifdef NO_MD5CRYPT_1
00166 if (use1 || useapr1) badopt = 1;
00167 #endif
00168
00169 if (badopt)
00170 {
00171 BIO_printf(bio_err, "Usage: passwd [options] [passwords]\n");
00172 BIO_printf(bio_err, "where options are\n");
00173 #ifndef OPENSSL_NO_DES
00174 BIO_printf(bio_err, "-crypt standard Unix password algorithm (default)\n");
00175 #endif
00176 #ifndef NO_MD5CRYPT_1
00177 BIO_printf(bio_err, "-1 MD5-based password algorithm\n");
00178 BIO_printf(bio_err, "-apr1 MD5-based password algorithm, Apache variant\n");
00179 #endif
00180 BIO_printf(bio_err, "-salt string use provided salt\n");
00181 BIO_printf(bio_err, "-in file read passwords from file\n");
00182 BIO_printf(bio_err, "-stdin read passwords from stdin\n");
00183 BIO_printf(bio_err, "-noverify never verify when reading password from terminal\n");
00184 BIO_printf(bio_err, "-quiet no warnings\n");
00185 BIO_printf(bio_err, "-table format output as table\n");
00186 BIO_printf(bio_err, "-reverse switch table columns\n");
00187
00188 goto err;
00189 }
00190
00191 if ((infile != NULL) || in_stdin)
00192 {
00193 in = BIO_new(BIO_s_file());
00194 if (in == NULL)
00195 goto err;
00196 if (infile != NULL)
00197 {
00198 assert(in_stdin == 0);
00199 if (BIO_read_filename(in, infile) <= 0)
00200 goto err;
00201 }
00202 else
00203 {
00204 assert(in_stdin);
00205 BIO_set_fp(in, stdin, BIO_NOCLOSE);
00206 }
00207 }
00208
00209 if (usecrypt)
00210 pw_maxlen = 8;
00211 else if (use1 || useapr1)
00212 pw_maxlen = 256;
00213
00214 if (passwds == NULL)
00215 {
00216
00217
00218 passwd_malloc_size = pw_maxlen + 2;
00219
00220 passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
00221 if (passwd_malloc == NULL)
00222 goto err;
00223 }
00224
00225 if ((in == NULL) && (passwds == NULL))
00226 {
00227
00228 static char *passwds_static[2] = {NULL, NULL};
00229
00230 passwds = passwds_static;
00231 if (in == NULL)
00232 if (EVP_read_pw_string(passwd_malloc, passwd_malloc_size, "Password: ", !(passed_salt || in_noverify)) != 0)
00233 goto err;
00234 passwds[0] = passwd_malloc;
00235 }
00236
00237 if (in == NULL)
00238 {
00239 assert(passwds != NULL);
00240 assert(*passwds != NULL);
00241
00242 do
00243 {
00244 passwd = *passwds++;
00245 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
00246 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
00247 goto err;
00248 }
00249 while (*passwds != NULL);
00250 }
00251 else
00252
00253 {
00254 int done;
00255
00256 assert (passwd != NULL);
00257 do
00258 {
00259 int r = BIO_gets(in, passwd, pw_maxlen + 1);
00260 if (r > 0)
00261 {
00262 char *c = (strchr(passwd, '\n')) ;
00263 if (c != NULL)
00264 *c = 0;
00265 else
00266 {
00267
00268 char trash[BUFSIZ];
00269 do
00270 r = BIO_gets(in, trash, sizeof trash);
00271 while ((r > 0) && (!strchr(trash, '\n')));
00272 }
00273
00274 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, out,
00275 quiet, table, reverse, pw_maxlen, usecrypt, use1, useapr1))
00276 goto err;
00277 }
00278 done = (r <= 0);
00279 }
00280 while (!done);
00281 }
00282 ret = 0;
00283
00284 err:
00285 ERR_print_errors(bio_err);
00286 if (salt_malloc)
00287 OPENSSL_free(salt_malloc);
00288 if (passwd_malloc)
00289 OPENSSL_free(passwd_malloc);
00290 if (in)
00291 BIO_free(in);
00292 if (out)
00293 BIO_free_all(out);
00294 apps_shutdown();
00295 OPENSSL_EXIT(ret);
00296 }
00297
00298
00299 #ifndef NO_MD5CRYPT_1
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
00311 {
00312 static char out_buf[6 + 9 + 24 + 2];
00313 unsigned char buf[MD5_DIGEST_LENGTH];
00314 char *salt_out;
00315 int n;
00316 unsigned int i;
00317 EVP_MD_CTX md,md2;
00318 size_t passwd_len, salt_len;
00319
00320 passwd_len = strlen(passwd);
00321 out_buf[0] = '$';
00322 out_buf[1] = 0;
00323 assert(strlen(magic) <= 4);
00324 strncat(out_buf, magic, 4);
00325 strncat(out_buf, "$", 1);
00326 strncat(out_buf, salt, 8);
00327 assert(strlen(out_buf) <= 6 + 8);
00328 salt_out = out_buf + 2 + strlen(magic);
00329 salt_len = strlen(salt_out);
00330 assert(salt_len <= 8);
00331
00332 EVP_MD_CTX_init(&md);
00333 EVP_DigestInit_ex(&md,EVP_md5(), NULL);
00334 EVP_DigestUpdate(&md, passwd, passwd_len);
00335 EVP_DigestUpdate(&md, "$", 1);
00336 EVP_DigestUpdate(&md, magic, strlen(magic));
00337 EVP_DigestUpdate(&md, "$", 1);
00338 EVP_DigestUpdate(&md, salt_out, salt_len);
00339
00340 EVP_MD_CTX_init(&md2);
00341 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
00342 EVP_DigestUpdate(&md2, passwd, passwd_len);
00343 EVP_DigestUpdate(&md2, salt_out, salt_len);
00344 EVP_DigestUpdate(&md2, passwd, passwd_len);
00345 EVP_DigestFinal_ex(&md2, buf, NULL);
00346
00347 for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
00348 EVP_DigestUpdate(&md, buf, sizeof buf);
00349 EVP_DigestUpdate(&md, buf, i);
00350
00351 n = passwd_len;
00352 while (n)
00353 {
00354 EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
00355 n >>= 1;
00356 }
00357 EVP_DigestFinal_ex(&md, buf, NULL);
00358
00359 for (i = 0; i < 1000; i++)
00360 {
00361 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
00362 EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf,
00363 (i & 1) ? passwd_len : sizeof buf);
00364 if (i % 3)
00365 EVP_DigestUpdate(&md2, salt_out, salt_len);
00366 if (i % 7)
00367 EVP_DigestUpdate(&md2, passwd, passwd_len);
00368 EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd,
00369 (i & 1) ? sizeof buf : passwd_len);
00370 EVP_DigestFinal_ex(&md2, buf, NULL);
00371 }
00372 EVP_MD_CTX_cleanup(&md2);
00373
00374 {
00375
00376
00377 unsigned char buf_perm[sizeof buf];
00378 int dest, source;
00379 char *output;
00380
00381
00382 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
00383 buf_perm[dest] = buf[source];
00384 buf_perm[14] = buf[5];
00385 buf_perm[15] = buf[11];
00386 #ifndef PEDANTIC
00387 assert(16 == sizeof buf_perm);
00388 #endif
00389
00390 output = salt_out + salt_len;
00391 assert(output == out_buf + strlen(out_buf));
00392
00393 *output++ = '$';
00394
00395 for (i = 0; i < 15; i += 3)
00396 {
00397 *output++ = cov_2char[buf_perm[i+2] & 0x3f];
00398 *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
00399 (buf_perm[i+2] >> 6)];
00400 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
00401 (buf_perm[i+1] >> 4)];
00402 *output++ = cov_2char[buf_perm[i] >> 2];
00403 }
00404 assert(i == 15);
00405 *output++ = cov_2char[buf_perm[i] & 0x3f];
00406 *output++ = cov_2char[buf_perm[i] >> 6];
00407 *output = 0;
00408 assert(strlen(out_buf) < sizeof(out_buf));
00409 }
00410 EVP_MD_CTX_cleanup(&md);
00411
00412 return out_buf;
00413 }
00414 #endif
00415
00416
00417 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
00418 char *passwd, BIO *out, int quiet, int table, int reverse,
00419 size_t pw_maxlen, int usecrypt, int use1, int useapr1)
00420 {
00421 char *hash = NULL;
00422
00423 assert(salt_p != NULL);
00424 assert(salt_malloc_p != NULL);
00425
00426
00427 if (!passed_salt)
00428 {
00429 #ifndef OPENSSL_NO_DES
00430 if (usecrypt)
00431 {
00432 if (*salt_malloc_p == NULL)
00433 {
00434 *salt_p = *salt_malloc_p = OPENSSL_malloc(3);
00435 if (*salt_malloc_p == NULL)
00436 goto err;
00437 }
00438 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
00439 goto err;
00440 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f];
00441 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f];
00442 (*salt_p)[2] = 0;
00443 #ifdef CHARSET_EBCDIC
00444 ascii2ebcdic(*salt_p, *salt_p, 2);
00445
00446 #endif
00447 }
00448 #endif
00449
00450 #ifndef NO_MD5CRYPT_1
00451 if (use1 || useapr1)
00452 {
00453 int i;
00454
00455 if (*salt_malloc_p == NULL)
00456 {
00457 *salt_p = *salt_malloc_p = OPENSSL_malloc(9);
00458 if (*salt_malloc_p == NULL)
00459 goto err;
00460 }
00461 if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
00462 goto err;
00463
00464 for (i = 0; i < 8; i++)
00465 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f];
00466 (*salt_p)[8] = 0;
00467 }
00468 #endif
00469 }
00470
00471 assert(*salt_p != NULL);
00472
00473
00474 if ((strlen(passwd) > pw_maxlen))
00475 {
00476 if (!quiet)
00477
00478 BIO_printf(bio_err, "Warning: truncating password to %u characters\n", (unsigned)pw_maxlen);
00479 passwd[pw_maxlen] = 0;
00480 }
00481 assert(strlen(passwd) <= pw_maxlen);
00482
00483
00484 #ifndef OPENSSL_NO_DES
00485 if (usecrypt)
00486 hash = DES_crypt(passwd, *salt_p);
00487 #endif
00488 #ifndef NO_MD5CRYPT_1
00489 if (use1 || useapr1)
00490 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
00491 #endif
00492 assert(hash != NULL);
00493
00494 if (table && !reverse)
00495 BIO_printf(out, "%s\t%s\n", passwd, hash);
00496 else if (table && reverse)
00497 BIO_printf(out, "%s\t%s\n", hash, passwd);
00498 else
00499 BIO_printf(out, "%s\n", hash);
00500 return 1;
00501
00502 err:
00503 return 0;
00504 }
00505 #else
00506
00507 int MAIN(int argc, char **argv)
00508 {
00509 fputs("Program not available.\n", stderr)
00510 OPENSSL_EXIT(1);
00511 }
00512 #endif