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