]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/app_rand.c
Don't use "version" in "OpenSSL Version 1.1.0" etc
[thirdparty/openssl.git] / apps / app_rand.c
CommitLineData
846e33c7 1/*
2234212c 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
640588bb 3 *
846e33c7
RS
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
640588bb
BM
8 */
9
a0ad17bb 10#include "apps.h"
640588bb
BM
11#include <openssl/bio.h>
12#include <openssl/rand.h>
13
640588bb 14static int seeded = 0;
4ec2d4d2 15static int egdsocket = 0;
640588bb 16
7e1b7485 17int app_RAND_load_file(const char *file, int dont_warn)
0f113f3e
MC
18{
19 int consider_randfile = (file == NULL);
20 char buffer[200];
21
2234212c 22 if (file == NULL) {
0f113f3e 23 file = RAND_file_name(buffer, sizeof buffer);
0423f812 24#ifndef OPENSSL_NO_EGD
2234212c 25 } else if (RAND_egd(file) > 0) {
0f113f3e
MC
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;
0423f812 32#endif
2234212c
PY
33 }
34
0f113f3e
MC
35 if (file == NULL || !RAND_load_file(file, -1)) {
36 if (RAND_status() == 0) {
37 if (!dont_warn) {
7e1b7485
RS
38 BIO_printf(bio_err, "unable to load 'random state'\n");
39 BIO_printf(bio_err,
0f113f3e 40 "This means that the random number generator has not been seeded\n");
7e1b7485 41 BIO_printf(bio_err, "with much random data.\n");
0f113f3e
MC
42 if (consider_randfile) { /* explanation does not apply when a
43 * file is explicitly named */
7e1b7485 44 BIO_printf(bio_err,
0f113f3e 45 "Consider setting the RANDFILE environment variable to point at a file that\n");
7e1b7485 46 BIO_printf(bio_err,
0f113f3e
MC
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}
640588bb
BM
56
57long app_RAND_load_files(char *name)
0f113f3e
MC
58{
59 char *p, *n;
60 int last;
61 long tot = 0;
0423f812 62#ifndef OPENSSL_NO_EGD
0f113f3e 63 int egd;
0423f812 64#endif
640588bb 65
0f113f3e
MC
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
0423f812 77#ifndef OPENSSL_NO_EGD
0f113f3e
MC
78 egd = RAND_egd(n);
79 if (egd > 0)
80 tot += egd;
81 else
0423f812 82#endif
0f113f3e
MC
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}
640588bb 91
7e1b7485 92int app_RAND_write_file(const char *file)
0f113f3e
MC
93{
94 char buffer[200];
95
f367ac2b 96 if (egdsocket || !seeded) {
0f113f3e 97 /*
f367ac2b
RS
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.
0f113f3e
MC
101 */
102 return 0;
f367ac2b 103 }
640588bb 104
0f113f3e
MC
105 if (file == NULL)
106 file = RAND_file_name(buffer, sizeof buffer);
107 if (file == NULL || !RAND_write_file(file)) {
7e1b7485 108 BIO_printf(bio_err, "unable to write 'random state'\n");
0f113f3e
MC
109 return 0;
110 }
111 return 1;
112}
640588bb
BM
113
114void app_RAND_allow_write_file(void)
0f113f3e
MC
115{
116 seeded = 1;
117}