159191Skris/* apps/app_rand.c */
259191Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
359191Skris * All rights reserved.
459191Skris *
559191Skris * This package is an SSL implementation written
659191Skris * by Eric Young (eay@cryptsoft.com).
759191Skris * The implementation was written so as to conform with Netscapes SSL.
859191Skris *
959191Skris * This library is free for commercial and non-commercial use as long as
1059191Skris * the following conditions are aheared to.  The following conditions
1159191Skris * apply to all code found in this distribution, be it the RC4, RSA,
1259191Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1359191Skris * included with this distribution is covered by the same copyright terms
1459191Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1559191Skris *
1659191Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1759191Skris * the code are not to be removed.
1859191Skris * If this package is used in a product, Eric Young should be given attribution
1959191Skris * as the author of the parts of the library used.
2059191Skris * This can be in the form of a textual message at program startup or
2159191Skris * in documentation (online or textual) provided with the package.
2259191Skris *
2359191Skris * Redistribution and use in source and binary forms, with or without
2459191Skris * modification, are permitted provided that the following conditions
2559191Skris * are met:
2659191Skris * 1. Redistributions of source code must retain the copyright
2759191Skris *    notice, this list of conditions and the following disclaimer.
2859191Skris * 2. Redistributions in binary form must reproduce the above copyright
2959191Skris *    notice, this list of conditions and the following disclaimer in the
3059191Skris *    documentation and/or other materials provided with the distribution.
3159191Skris * 3. All advertising materials mentioning features or use of this software
3259191Skris *    must display the following acknowledgement:
3359191Skris *    "This product includes cryptographic software written by
3459191Skris *     Eric Young (eay@cryptsoft.com)"
3559191Skris *    The word 'cryptographic' can be left out if the rouines from the library
3659191Skris *    being used are not cryptographic related :-).
3759191Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3859191Skris *    the apps directory (application code) you must include an acknowledgement:
3959191Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4059191Skris *
4159191Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4259191Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4359191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4459191Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4559191Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4659191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4759191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4859191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4959191Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5059191Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5159191Skris * SUCH DAMAGE.
5259191Skris *
5359191Skris * The licence and distribution terms for any publically available version or
5459191Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5559191Skris * copied and put under another distribution licence
5659191Skris * [including the GNU Public Licence.]
5759191Skris */
5859191Skris/* ====================================================================
5959191Skris * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
6059191Skris *
6159191Skris * Redistribution and use in source and binary forms, with or without
6259191Skris * modification, are permitted provided that the following conditions
6359191Skris * are met:
6459191Skris *
6559191Skris * 1. Redistributions of source code must retain the above copyright
6659191Skris *    notice, this list of conditions and the following disclaimer.
6759191Skris *
6859191Skris * 2. Redistributions in binary form must reproduce the above copyright
6959191Skris *    notice, this list of conditions and the following disclaimer in
7059191Skris *    the documentation and/or other materials provided with the
7159191Skris *    distribution.
7259191Skris *
7359191Skris * 3. All advertising materials mentioning features or use of this
7459191Skris *    software must display the following acknowledgment:
7559191Skris *    "This product includes software developed by the OpenSSL Project
7659191Skris *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
7759191Skris *
7859191Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7959191Skris *    endorse or promote products derived from this software without
8059191Skris *    prior written permission. For written permission, please contact
8159191Skris *    openssl-core@openssl.org.
8259191Skris *
8359191Skris * 5. Products derived from this software may not be called "OpenSSL"
8459191Skris *    nor may "OpenSSL" appear in their names without prior written
8559191Skris *    permission of the OpenSSL Project.
8659191Skris *
8759191Skris * 6. Redistributions of any form whatsoever must retain the following
8859191Skris *    acknowledgment:
8959191Skris *    "This product includes software developed by the OpenSSL Project
9059191Skris *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
9159191Skris *
9259191Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9359191Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9459191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9559191Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
9659191Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9759191Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9859191Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9959191Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10059191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10159191Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10259191Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10359191Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
10459191Skris * ====================================================================
10559191Skris *
10659191Skris * This product includes cryptographic software written by Eric Young
10759191Skris * (eay@cryptsoft.com).  This product includes software written by Tim
10859191Skris * Hudson (tjh@cryptsoft.com).
10959191Skris *
11059191Skris */
11159191Skris
11259191Skris#define NON_MAIN
11359191Skris#include "apps.h"
11459191Skris#undef NON_MAIN
11559191Skris#include <openssl/bio.h>
11659191Skris#include <openssl/rand.h>
11759191Skris
11859191Skris
11959191Skrisstatic int seeded = 0;
12059191Skrisstatic int egdsocket = 0;
12159191Skris
12259191Skrisint app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn)
12359191Skris	{
12459191Skris	int consider_randfile = (file == NULL);
12559191Skris	char buffer[200];
12659191Skris
127109998Smarkm#ifdef OPENSSL_SYS_WINDOWS
12859191Skris	BIO_printf(bio_e,"Loading 'screen' into random state -");
12959191Skris	BIO_flush(bio_e);
13059191Skris	RAND_screen();
13159191Skris	BIO_printf(bio_e," done\n");
13259191Skris#endif
13359191Skris
13459191Skris	if (file == NULL)
13559191Skris		file = RAND_file_name(buffer, sizeof buffer);
13659191Skris	else if (RAND_egd(file) > 0)
13759191Skris		{
13859191Skris		/* we try if the given filename is an EGD socket.
13959191Skris		   if it is, we don't write anything back to the file. */
14059191Skris		egdsocket = 1;
14159191Skris		return 1;
14259191Skris		}
14359191Skris	if (file == NULL || !RAND_load_file(file, -1))
14459191Skris		{
145109998Smarkm		if (RAND_status() == 0)
14659191Skris			{
147109998Smarkm			if (!dont_warn)
14859191Skris				{
149109998Smarkm				BIO_printf(bio_e,"unable to load 'random state'\n");
150109998Smarkm				BIO_printf(bio_e,"This means that the random number generator has not been seeded\n");
151109998Smarkm				BIO_printf(bio_e,"with much random data.\n");
152109998Smarkm				if (consider_randfile) /* explanation does not apply when a file is explicitly named */
153109998Smarkm					{
154109998Smarkm					BIO_printf(bio_e,"Consider setting the RANDFILE environment variable to point at a file that\n");
155109998Smarkm					BIO_printf(bio_e,"'random' data can be kept in (the file will be overwritten).\n");
156109998Smarkm					}
15759191Skris				}
158109998Smarkm			return 0;
15959191Skris			}
16059191Skris		}
16159191Skris	seeded = 1;
16259191Skris	return 1;
16359191Skris	}
16459191Skris
16559191Skrislong app_RAND_load_files(char *name)
16659191Skris	{
16759191Skris	char *p,*n;
16859191Skris	int last;
16959191Skris	long tot=0;
17059191Skris	int egd;
17159191Skris
17259191Skris	for (;;)
17359191Skris		{
17459191Skris		last=0;
17559191Skris		for (p=name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++);
17659191Skris		if (*p == '\0') last=1;
17759191Skris		*p='\0';
17859191Skris		n=name;
17959191Skris		name=p+1;
18059191Skris		if (*n == '\0') break;
18159191Skris
18259191Skris		egd=RAND_egd(n);
18372613Skris		if (egd > 0)
18472613Skris			tot+=egd;
18572613Skris		else
18672613Skris			tot+=RAND_load_file(n,-1);
18759191Skris		if (last) break;
18859191Skris		}
18959191Skris	if (tot > 512)
19059191Skris		app_RAND_allow_write_file();
19159191Skris	return(tot);
19259191Skris	}
19359191Skris
19459191Skrisint app_RAND_write_file(const char *file, BIO *bio_e)
19559191Skris	{
19659191Skris	char buffer[200];
19759191Skris
19859191Skris	if (egdsocket || !seeded)
19959191Skris		/* If we did not manage to read the seed file,
20059191Skris		 * we should not write a low-entropy seed file back --
20159191Skris		 * it would suppress a crucial warning the next time
20259191Skris		 * we want to use it. */
20359191Skris		return 0;
20459191Skris
20559191Skris	if (file == NULL)
20659191Skris		file = RAND_file_name(buffer, sizeof buffer);
20759191Skris	if (file == NULL || !RAND_write_file(file))
20859191Skris		{
20959191Skris		BIO_printf(bio_e,"unable to write 'random state'\n");
21059191Skris		return 0;
21159191Skris		}
21259191Skris	return 1;
21359191Skris	}
21459191Skris
21559191Skrisvoid app_RAND_allow_write_file(void)
21659191Skris	{
21759191Skris	seeded = 1;
21859191Skris	}
219