21445ac0f9456e0da8d7a32df427337376a7df43
[openssl.git] / apps / app_rand.c
1 /*
2  * Copyright 1995-2017 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     if (file == NULL) {
23         file = RAND_file_name(buffer, sizeof buffer);
24 #ifndef OPENSSL_NO_EGD
25     } else if (RAND_egd(file) > 0) {
26         /*
27          * we try if the given filename is an EGD socket. if it is, we don't
28          * write anything back to the file.
29          */
30         egdsocket = 1;
31         return 1;
32 #endif
33     }
34
35     if (file == NULL || !RAND_load_file(file, -1)) {
36         if (RAND_status() == 0) {
37             if (!dont_warn) {
38                 BIO_printf(bio_err, "unable to load 'random state'\n");
39                 BIO_printf(bio_err,
40                            "This means that the random number generator has not been seeded\n");
41                 BIO_printf(bio_err, "with much random data.\n");
42                 if (consider_randfile) { /* explanation does not apply when a
43                                           * file is explicitly named */
44                     BIO_printf(bio_err,
45                                "Consider setting the RANDFILE environment variable to point at a file that\n");
46                     BIO_printf(bio_err,
47                                "'random' data can be kept in (the file will be overwritten).\n");
48                 }
49             }
50             return 0;
51         }
52     }
53     seeded = 1;
54     return 1;
55 }
56
57 long app_RAND_load_files(char *name)
58 {
59     char *p, *n;
60     int last;
61     long tot = 0;
62 #ifndef OPENSSL_NO_EGD
63     int egd;
64 #endif
65
66     for (;;) {
67         last = 0;
68         for (p = name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++) ;
69         if (*p == '\0')
70             last = 1;
71         *p = '\0';
72         n = name;
73         name = p + 1;
74         if (*n == '\0')
75             break;
76
77 #ifndef OPENSSL_NO_EGD
78         egd = RAND_egd(n);
79         if (egd > 0)
80             tot += egd;
81         else
82 #endif
83             tot += RAND_load_file(n, -1);
84         if (last)
85             break;
86     }
87     if (tot > 512)
88         app_RAND_allow_write_file();
89     return (tot);
90 }
91
92 int app_RAND_write_file(const char *file)
93 {
94     char buffer[200];
95
96     if (egdsocket || !seeded) {
97         /*
98          * If we didn't manage to read the seed file, don't write a
99          * file out -- it would suppress a crucial warning the next
100          * time we want to use it.
101          */
102         return 0;
103     }
104
105     if (file == NULL)
106         file = RAND_file_name(buffer, sizeof buffer);
107     if (file == NULL || !RAND_write_file(file)) {
108         BIO_printf(bio_err, "unable to write 'random state'\n");
109         return 0;
110     }
111     return 1;
112 }
113
114 void app_RAND_allow_write_file(void)
115 {
116     seeded = 1;
117 }