]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/app_rand.c
Fix srp app missing NULL termination with password callback
[thirdparty/openssl.git] / apps / app_rand.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 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
0f113f3e
MC
22 if (file == NULL)
23 file = RAND_file_name(buffer, sizeof buffer);
0423f812 24#ifndef OPENSSL_NO_EGD
0f113f3e
MC
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 }
0423f812 33#endif
0f113f3e
MC
34 if (file == NULL || !RAND_load_file(file, -1)) {
35 if (RAND_status() == 0) {
36 if (!dont_warn) {
7e1b7485
RS
37 BIO_printf(bio_err, "unable to load 'random state'\n");
38 BIO_printf(bio_err,
0f113f3e 39 "This means that the random number generator has not been seeded\n");
7e1b7485 40 BIO_printf(bio_err, "with much random data.\n");
0f113f3e
MC
41 if (consider_randfile) { /* explanation does not apply when a
42 * file is explicitly named */
7e1b7485 43 BIO_printf(bio_err,
0f113f3e 44 "Consider setting the RANDFILE environment variable to point at a file that\n");
7e1b7485 45 BIO_printf(bio_err,
0f113f3e
MC
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}
640588bb
BM
55
56long app_RAND_load_files(char *name)
0f113f3e
MC
57{
58 char *p, *n;
59 int last;
60 long tot = 0;
0423f812 61#ifndef OPENSSL_NO_EGD
0f113f3e 62 int egd;
0423f812 63#endif
640588bb 64
0f113f3e
MC
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
0423f812 76#ifndef OPENSSL_NO_EGD
0f113f3e
MC
77 egd = RAND_egd(n);
78 if (egd > 0)
79 tot += egd;
80 else
0423f812 81#endif
0f113f3e
MC
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}
640588bb 90
7e1b7485 91int app_RAND_write_file(const char *file)
0f113f3e
MC
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;
640588bb 102
0f113f3e
MC
103 if (file == NULL)
104 file = RAND_file_name(buffer, sizeof buffer);
105 if (file == NULL || !RAND_write_file(file)) {
7e1b7485 106 BIO_printf(bio_err, "unable to write 'random state'\n");
0f113f3e
MC
107 return 0;
108 }
109 return 1;
110}
640588bb
BM
111
112void app_RAND_allow_write_file(void)
0f113f3e
MC
113{
114 seeded = 1;
115}