Cleanup openssl.ec
[openssl.git] / apps / app_rand.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "apps.h"
11 #include <openssl/bio.h>
12 #include <openssl/rand.h>
13
14 static int seeded = 0;
15 static int egdsocket = 0;
16
17 int app_RAND_load_file(const char *file, int dont_warn)
18 {
19     int consider_randfile = (file == NULL);
20     char buffer[200];
21
22 #ifdef OPENSSL_SYS_WINDOWS
23     RAND_screen();
24 #endif
25
26     if (file == NULL)
27         file = RAND_file_name(buffer, sizeof buffer);
28 #ifndef OPENSSL_NO_EGD
29     else if (RAND_egd(file) > 0) {
30         /*
31          * we try if the given filename is an EGD socket. if it is, we don't
32          * write anything back to the file.
33          */
34         egdsocket = 1;
35         return 1;
36     }
37 #endif
38     if (file == NULL || !RAND_load_file(file, -1)) {
39         if (RAND_status() == 0) {
40             if (!dont_warn) {
41                 BIO_printf(bio_err, "unable to load 'random state'\n");
42                 BIO_printf(bio_err,
43                            "This means that the random number generator has not been seeded\n");
44                 BIO_printf(bio_err, "with much random data.\n");
45                 if (consider_randfile) { /* explanation does not apply when a
46                                           * file is explicitly named */
47                     BIO_printf(bio_err,
48                                "Consider setting the RANDFILE environment variable to point at a file that\n");
49                     BIO_printf(bio_err,
50                                "'random' data can be kept in (the file will be overwritten).\n");
51                 }
52             }
53             return 0;
54         }
55     }
56     seeded = 1;
57     return 1;
58 }
59
60 long app_RAND_load_files(char *name)
61 {
62     char *p, *n;
63     int last;
64     long tot = 0;
65 #ifndef OPENSSL_NO_EGD
66     int egd;
67 #endif
68
69     for (;;) {
70         last = 0;
71         for (p = name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++) ;
72         if (*p == '\0')
73             last = 1;
74         *p = '\0';
75         n = name;
76         name = p + 1;
77         if (*n == '\0')
78             break;
79
80 #ifndef OPENSSL_NO_EGD
81         egd = RAND_egd(n);
82         if (egd > 0)
83             tot += egd;
84         else
85 #endif
86             tot += RAND_load_file(n, -1);
87         if (last)
88             break;
89     }
90     if (tot > 512)
91         app_RAND_allow_write_file();
92     return (tot);
93 }
94
95 int app_RAND_write_file(const char *file)
96 {
97     char buffer[200];
98
99     if (egdsocket || !seeded)
100         /*
101          * If we did not manage to read the seed file, we should not write a
102          * low-entropy seed file back -- it would suppress a crucial warning
103          * the next time we want to use it.
104          */
105         return 0;
106
107     if (file == NULL)
108         file = RAND_file_name(buffer, sizeof buffer);
109     if (file == NULL || !RAND_write_file(file)) {
110         BIO_printf(bio_err, "unable to write 'random state'\n");
111         return 0;
112     }
113     return 1;
114 }
115
116 void app_RAND_allow_write_file(void)
117 {
118     seeded = 1;
119 }