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