app_rand.c revision 280304
167754Smsmith/* apps/app_rand.c */
267754Smsmith/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
367754Smsmith * All rights reserved.
4138287Smarks *
567754Smsmith * This package is an SSL implementation written
667754Smsmith * by Eric Young (eay@cryptsoft.com).
767754Smsmith * The implementation was written so as to conform with Netscapes SSL.
867754Smsmith *
967754Smsmith * This library is free for commercial and non-commercial use as long as
1067754Smsmith * the following conditions are aheared to.  The following conditions
1167754Smsmith * apply to all code found in this distribution, be it the RC4, RSA,
12126372Snjl * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1370243Smsmith * included with this distribution is covered by the same copyright terms
1467754Smsmith * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1567754Smsmith *
1667754Smsmith * Copyright remains Eric Young's, and as such any Copyright notices in
1767754Smsmith * the code are not to be removed.
1867754Smsmith * If this package is used in a product, Eric Young should be given attribution
1967754Smsmith * as the author of the parts of the library used.
2067754Smsmith * This can be in the form of a textual message at program startup or
2167754Smsmith * in documentation (online or textual) provided with the package.
2267754Smsmith *
2367754Smsmith * Redistribution and use in source and binary forms, with or without
2467754Smsmith * modification, are permitted provided that the following conditions
2567754Smsmith * are met:
2667754Smsmith * 1. Redistributions of source code must retain the copyright
2767754Smsmith *    notice, this list of conditions and the following disclaimer.
2867754Smsmith * 2. Redistributions in binary form must reproduce the above copyright
2967754Smsmith *    notice, this list of conditions and the following disclaimer in the
3067754Smsmith *    documentation and/or other materials provided with the distribution.
3167754Smsmith * 3. All advertising materials mentioning features or use of this software
3267754Smsmith *    must display the following acknowledgement:
3367754Smsmith *    "This product includes cryptographic software written by
3467754Smsmith *     Eric Young (eay@cryptsoft.com)"
3567754Smsmith *    The word 'cryptographic' can be left out if the rouines from the library
3667754Smsmith *    being used are not cryptographic related :-).
3767754Smsmith * 4. If you include any Windows specific code (or a derivative thereof) from
3867754Smsmith *    the apps directory (application code) you must include an acknowledgement:
3967754Smsmith *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4067754Smsmith *
4167754Smsmith * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4267754Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4367754Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4467754Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4567754Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4667754Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4767754Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4867754Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4967754Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5067754Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5167754Smsmith * SUCH DAMAGE.
5267754Smsmith *
5367754Smsmith * The licence and distribution terms for any publically available version or
5467754Smsmith * derivative of this code cannot be changed.  i.e. this code cannot simply be
5567754Smsmith * copied and put under another distribution licence
5667754Smsmith * [including the GNU Public Licence.]
5767754Smsmith */
5867754Smsmith/* ====================================================================
5967754Smsmith * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
6067754Smsmith *
6167754Smsmith * Redistribution and use in source and binary forms, with or without
6267754Smsmith * modification, are permitted provided that the following conditions
6367754Smsmith * are met:
6467754Smsmith *
6567754Smsmith * 1. Redistributions of source code must retain the above copyright
6667754Smsmith *    notice, this list of conditions and the following disclaimer.
6767754Smsmith *
6867754Smsmith * 2. Redistributions in binary form must reproduce the above copyright
6967754Smsmith *    notice, this list of conditions and the following disclaimer in
7067754Smsmith *    the documentation and/or other materials provided with the
7167754Smsmith *    distribution.
7267754Smsmith *
7367754Smsmith * 3. All advertising materials mentioning features or use of this
7467754Smsmith *    software must display the following acknowledgment:
7567754Smsmith *    "This product includes software developed by the OpenSSL Project
7667754Smsmith *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
7767754Smsmith *
7867754Smsmith * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7967754Smsmith *    endorse or promote products derived from this software without
8067754Smsmith *    prior written permission. For written permission, please contact
8167754Smsmith *    openssl-core@openssl.org.
8267754Smsmith *
8367754Smsmith * 5. Products derived from this software may not be called "OpenSSL"
8467754Smsmith *    nor may "OpenSSL" appear in their names without prior written
8567754Smsmith *    permission of the OpenSSL Project.
8667754Smsmith *
8767754Smsmith * 6. Redistributions of any form whatsoever must retain the following
8867754Smsmith *    acknowledgment:
8967754Smsmith *    "This product includes software developed by the OpenSSL Project
9067754Smsmith *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
9167754Smsmith *
9267754Smsmith * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9367754Smsmith * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9467754Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9567754Smsmith * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
9667754Smsmith * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9767754Smsmith * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9867754Smsmith * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9967754Smsmith * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10067754Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10167754Smsmith * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10267754Smsmith * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10367754Smsmith * OF THE POSSIBILITY OF SUCH DAMAGE.
10467754Smsmith * ====================================================================
10567754Smsmith *
10667754Smsmith * This product includes cryptographic software written by Eric Young
10767754Smsmith * (eay@cryptsoft.com).  This product includes software written by Tim
10867754Smsmith * Hudson (tjh@cryptsoft.com).
10967754Smsmith *
11067754Smsmith */
11167754Smsmith
11267754Smsmith#define NON_MAIN
11367754Smsmith#include "apps.h"
11467754Smsmith#undef NON_MAIN
11567754Smsmith#include <openssl/bio.h>
11667754Smsmith#include <openssl/rand.h>
11767754Smsmith
11867754Smsmithstatic int seeded = 0;
11967754Smsmithstatic int egdsocket = 0;
120151600Sobrien
121151600Sobrienint app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn)
122151600Sobrien{
12367754Smsmith    int consider_randfile = (file == NULL);
12467754Smsmith    char buffer[200];
12577424Smsmith
12691116Smsmith#ifdef OPENSSL_SYS_WINDOWS
12767754Smsmith    BIO_printf(bio_e, "Loading 'screen' into random state -");
128123315Snjl    BIO_flush(bio_e);
129102550Siwasaki    RAND_screen();
13067754Smsmith    BIO_printf(bio_e, " done\n");
13177424Smsmith#endif
13267754Smsmith
13387031Smsmith    if (file == NULL)
13487031Smsmith        file = RAND_file_name(buffer, sizeof buffer);
13587031Smsmith    else if (RAND_egd(file) > 0) {
13687031Smsmith        /*
13787031Smsmith         * we try if the given filename is an EGD socket. if it is, we don't
13887031Smsmith         * write anything back to the file.
13987031Smsmith         */
14087031Smsmith        egdsocket = 1;
14187031Smsmith        return 1;
14287031Smsmith    }
14387031Smsmith    if (file == NULL || !RAND_load_file(file, -1)) {
14487031Smsmith        if (RAND_status() == 0) {
14587031Smsmith            if (!dont_warn) {
14687031Smsmith                BIO_printf(bio_e, "unable to load 'random state'\n");
14799146Siwasaki                BIO_printf(bio_e,
14887031Smsmith                           "This means that the random number generator has not been seeded\n");
14987031Smsmith                BIO_printf(bio_e, "with much random data.\n");
15087031Smsmith                if (consider_randfile) { /* explanation does not apply when a
15187031Smsmith                                          * file is explicitly named */
15287031Smsmith                    BIO_printf(bio_e,
15387031Smsmith                               "Consider setting the RANDFILE environment variable to point at a file that\n");
15487031Smsmith                    BIO_printf(bio_e,
155123315Snjl                               "'random' data can be kept in (the file will be overwritten).\n");
15687031Smsmith                }
15787031Smsmith            }
15887031Smsmith            return 0;
15991116Smsmith        }
16087031Smsmith    }
16191116Smsmith    seeded = 1;
16291116Smsmith    return 1;
16391116Smsmith}
16491116Smsmith
16591116Smsmithlong app_RAND_load_files(char *name)
16691116Smsmith{
16791116Smsmith    char *p, *n;
16891116Smsmith    int last;
16987031Smsmith    long tot = 0;
17087031Smsmith    int egd;
17187031Smsmith
17287031Smsmith    for (;;) {
17387031Smsmith        last = 0;
17487031Smsmith        for (p = name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++) ;
17587031Smsmith        if (*p == '\0')
17687031Smsmith            last = 1;
17767754Smsmith        *p = '\0';
17867754Smsmith        n = name;
17967754Smsmith        name = p + 1;
18067754Smsmith        if (*n == '\0')
18167754Smsmith            break;
18267754Smsmith
18367754Smsmith        egd = RAND_egd(n);
18467754Smsmith        if (egd > 0)
18567754Smsmith            tot += egd;
18667754Smsmith        else
18777424Smsmith            tot += RAND_load_file(n, -1);
18867754Smsmith        if (last)
189114237Snjl            break;
19067754Smsmith    }
19167754Smsmith    if (tot > 512)
192114237Snjl        app_RAND_allow_write_file();
19367754Smsmith    return (tot);
19467754Smsmith}
19567754Smsmith
19667754Smsmithint app_RAND_write_file(const char *file, BIO *bio_e)
19791116Smsmith{
19867754Smsmith    char buffer[200];
19983174Smsmith
20067754Smsmith    if (egdsocket || !seeded)
20167754Smsmith        /*
20267754Smsmith         * If we did not manage to read the seed file, we should not write a
20367754Smsmith         * low-entropy seed file back -- it would suppress a crucial warning
204114237Snjl         * the next time we want to use it.
20567754Smsmith         */
20667754Smsmith        return 0;
20767754Smsmith
20867754Smsmith    if (file == NULL)
209114237Snjl        file = RAND_file_name(buffer, sizeof buffer);
210114237Snjl    if (file == NULL || !RAND_write_file(file)) {
211114237Snjl        BIO_printf(bio_e, "unable to write 'random state'\n");
21267754Smsmith        return 0;
21367754Smsmith    }
21467754Smsmith    return 1;
21577424Smsmith}
21667754Smsmith
21767754Smsmithvoid app_RAND_allow_write_file(void)
21867754Smsmith{
21967754Smsmith    seeded = 1;
22067754Smsmith}
22167754Smsmith