app_rand.c revision 280304
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
11859191Skrisstatic int seeded = 0;
11959191Skrisstatic int egdsocket = 0;
12059191Skris
12159191Skrisint app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn)
12259191Skris{
12359191Skris    int consider_randfile = (file == NULL);
12459191Skris    char buffer[200];
12559191Skris
12659191Skris#ifdef OPENSSL_SYS_WINDOWS
12759191Skris    BIO_printf(bio_e, "Loading 'screen' into random state -");
12859191Skris    BIO_flush(bio_e);
12959191Skris    RAND_screen();
13059191Skris    BIO_printf(bio_e, " done\n");
13159191Skris#endif
13259191Skris
13359191Skris    if (file == NULL)
13459191Skris        file = RAND_file_name(buffer, sizeof buffer);
13559191Skris    else if (RAND_egd(file) > 0) {
13659191Skris        /*
13759191Skris         * we try if the given filename is an EGD socket. if it is, we don't
13859191Skris         * write anything back to the file.
13959191Skris         */
14059191Skris        egdsocket = 1;
14159191Skris        return 1;
14259191Skris    }
14359191Skris    if (file == NULL || !RAND_load_file(file, -1)) {
14459191Skris        if (RAND_status() == 0) {
14559191Skris            if (!dont_warn) {
14659191Skris                BIO_printf(bio_e, "unable to load 'random state'\n");
14759191Skris                BIO_printf(bio_e,
14859191Skris                           "This means that the random number generator has not been seeded\n");
14959191Skris                BIO_printf(bio_e, "with much random data.\n");
15059191Skris                if (consider_randfile) { /* explanation does not apply when a
15159191Skris                                          * file is explicitly named */
15259191Skris                    BIO_printf(bio_e,
15359191Skris                               "Consider setting the RANDFILE environment variable to point at a file that\n");
15459191Skris                    BIO_printf(bio_e,
15559191Skris                               "'random' data can be kept in (the file will be overwritten).\n");
15659191Skris                }
15759191Skris            }
15859191Skris            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        last = 0;
17459191Skris        for (p = name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++) ;
17559191Skris        if (*p == '\0')
17659191Skris            last = 1;
17759191Skris        *p = '\0';
17859191Skris        n = name;
17959191Skris        name = p + 1;
18059191Skris        if (*n == '\0')
18159191Skris            break;
18259191Skris
18359191Skris        egd = RAND_egd(n);
18459191Skris        if (egd > 0)
18559191Skris            tot += egd;
18659191Skris        else
18759191Skris            tot += RAND_load_file(n, -1);
18859191Skris        if (last)
18959191Skris            break;
19059191Skris    }
19159191Skris    if (tot > 512)
19259191Skris        app_RAND_allow_write_file();
19359191Skris    return (tot);
19459191Skris}
19559191Skris
19659191Skrisint app_RAND_write_file(const char *file, BIO *bio_e)
19759191Skris{
19859191Skris    char buffer[200];
19959191Skris
20059191Skris    if (egdsocket || !seeded)
20159191Skris        /*
20259191Skris         * If we did not manage to read the seed file, we should not write a
20359191Skris         * low-entropy seed file back -- it would suppress a crucial warning
20459191Skris         * the next time we want to use it.
20559191Skris         */
20659191Skris        return 0;
20759191Skris
20859191Skris    if (file == NULL)
20959191Skris        file = RAND_file_name(buffer, sizeof buffer);
21059191Skris    if (file == NULL || !RAND_write_file(file)) {
21159191Skris        BIO_printf(bio_e, "unable to write 'random state'\n");
21259191Skris        return 0;
21359191Skris    }
214    return 1;
215}
216
217void app_RAND_allow_write_file(void)
218{
219    seeded = 1;
220}
221