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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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
66280304Sjkim *    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
11859191Skrisstatic int seeded = 0;
11959191Skrisstatic int egdsocket = 0;
12059191Skris
12159191Skrisint app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn)
122280304Sjkim{
123280304Sjkim    int consider_randfile = (file == NULL);
124280304Sjkim    char buffer[200];
125280304Sjkim
126109998Smarkm#ifdef OPENSSL_SYS_WINDOWS
127280304Sjkim    BIO_printf(bio_e, "Loading 'screen' into random state -");
128280304Sjkim    BIO_flush(bio_e);
129280304Sjkim    RAND_screen();
130280304Sjkim    BIO_printf(bio_e, " done\n");
13159191Skris#endif
13259191Skris
133280304Sjkim    if (file == NULL)
134280304Sjkim        file = RAND_file_name(buffer, sizeof buffer);
135280304Sjkim    else if (RAND_egd(file) > 0) {
136280304Sjkim        /*
137280304Sjkim         * we try if the given filename is an EGD socket. if it is, we don't
138280304Sjkim         * write anything back to the file.
139280304Sjkim         */
140280304Sjkim        egdsocket = 1;
141280304Sjkim        return 1;
142280304Sjkim    }
143280304Sjkim    if (file == NULL || !RAND_load_file(file, -1)) {
144280304Sjkim        if (RAND_status() == 0) {
145280304Sjkim            if (!dont_warn) {
146280304Sjkim                BIO_printf(bio_e, "unable to load 'random state'\n");
147280304Sjkim                BIO_printf(bio_e,
148280304Sjkim                           "This means that the random number generator has not been seeded\n");
149280304Sjkim                BIO_printf(bio_e, "with much random data.\n");
150280304Sjkim                if (consider_randfile) { /* explanation does not apply when a
151280304Sjkim                                          * file is explicitly named */
152280304Sjkim                    BIO_printf(bio_e,
153280304Sjkim                               "Consider setting the RANDFILE environment variable to point at a file that\n");
154280304Sjkim                    BIO_printf(bio_e,
155280304Sjkim                               "'random' data can be kept in (the file will be overwritten).\n");
156280304Sjkim                }
157280304Sjkim            }
158280304Sjkim            return 0;
159280304Sjkim        }
160280304Sjkim    }
161280304Sjkim    seeded = 1;
162280304Sjkim    return 1;
163280304Sjkim}
16459191Skris
16559191Skrislong app_RAND_load_files(char *name)
166280304Sjkim{
167280304Sjkim    char *p, *n;
168280304Sjkim    int last;
169280304Sjkim    long tot = 0;
170280304Sjkim    int egd;
17159191Skris
172280304Sjkim    for (;;) {
173280304Sjkim        last = 0;
174280304Sjkim        for (p = name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++) ;
175280304Sjkim        if (*p == '\0')
176280304Sjkim            last = 1;
177280304Sjkim        *p = '\0';
178280304Sjkim        n = name;
179280304Sjkim        name = p + 1;
180280304Sjkim        if (*n == '\0')
181280304Sjkim            break;
18259191Skris
183280304Sjkim        egd = RAND_egd(n);
184280304Sjkim        if (egd > 0)
185280304Sjkim            tot += egd;
186280304Sjkim        else
187280304Sjkim            tot += RAND_load_file(n, -1);
188280304Sjkim        if (last)
189280304Sjkim            break;
190280304Sjkim    }
191280304Sjkim    if (tot > 512)
192280304Sjkim        app_RAND_allow_write_file();
193280304Sjkim    return (tot);
194280304Sjkim}
195280304Sjkim
19659191Skrisint app_RAND_write_file(const char *file, BIO *bio_e)
197280304Sjkim{
198280304Sjkim    char buffer[200];
19959191Skris
200280304Sjkim    if (egdsocket || !seeded)
201280304Sjkim        /*
202280304Sjkim         * If we did not manage to read the seed file, we should not write a
203280304Sjkim         * low-entropy seed file back -- it would suppress a crucial warning
204280304Sjkim         * the next time we want to use it.
205280304Sjkim         */
206280304Sjkim        return 0;
20759191Skris
208280304Sjkim    if (file == NULL)
209280304Sjkim        file = RAND_file_name(buffer, sizeof buffer);
210280304Sjkim    if (file == NULL || !RAND_write_file(file)) {
211280304Sjkim        BIO_printf(bio_e, "unable to write 'random state'\n");
212280304Sjkim        return 0;
213280304Sjkim    }
214280304Sjkim    return 1;
215280304Sjkim}
216280304Sjkim
21759191Skrisvoid app_RAND_allow_write_file(void)
218280304Sjkim{
219280304Sjkim    seeded = 1;
220280304Sjkim}
221