blob: 0d44af903b813518a44257207f6771763376e206 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Bodo Möller640588b1999-10-26 01:59:11 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * 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
Bodo Möller640588b1999-10-26 01:59:11 +00008 */
9
Dr. Stephen Hensona0ad17b1999-11-08 13:58:08 +000010#include "apps.h"
Bodo Möller640588b1999-10-26 01:59:11 +000011#include <openssl/bio.h>
12#include <openssl/rand.h>
13
Bodo Möller640588b1999-10-26 01:59:11 +000014static int seeded = 0;
Ulf Möller4ec2d4d2000-02-24 02:51:47 +000015static int egdsocket = 0;
Bodo Möller640588b1999-10-26 01:59:11 +000016
Rich Salz7e1b7482015-04-24 15:26:15 -040017int app_RAND_load_file(const char *file, int dont_warn)
Matt Caswell0f113f32015-01-22 03:40:55 +000018{
19 int consider_randfile = (file == NULL);
20 char buffer[200];
21
Matt Caswell0f113f32015-01-22 03:40:55 +000022 if (file == NULL)
23 file = RAND_file_name(buffer, sizeof buffer);
Benjamin Kaduk0423f812016-01-12 18:02:16 -060024#ifndef OPENSSL_NO_EGD
Matt Caswell0f113f32015-01-22 03:40:55 +000025 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 }
Benjamin Kaduk0423f812016-01-12 18:02:16 -060033#endif
Matt Caswell0f113f32015-01-22 03:40:55 +000034 if (file == NULL || !RAND_load_file(file, -1)) {
35 if (RAND_status() == 0) {
36 if (!dont_warn) {
Rich Salz7e1b7482015-04-24 15:26:15 -040037 BIO_printf(bio_err, "unable to load 'random state'\n");
38 BIO_printf(bio_err,
Matt Caswell0f113f32015-01-22 03:40:55 +000039 "This means that the random number generator has not been seeded\n");
Rich Salz7e1b7482015-04-24 15:26:15 -040040 BIO_printf(bio_err, "with much random data.\n");
Matt Caswell0f113f32015-01-22 03:40:55 +000041 if (consider_randfile) { /* explanation does not apply when a
42 * file is explicitly named */
Rich Salz7e1b7482015-04-24 15:26:15 -040043 BIO_printf(bio_err,
Matt Caswell0f113f32015-01-22 03:40:55 +000044 "Consider setting the RANDFILE environment variable to point at a file that\n");
Rich Salz7e1b7482015-04-24 15:26:15 -040045 BIO_printf(bio_err,
Matt Caswell0f113f32015-01-22 03:40:55 +000046 "'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}
Bodo Möller640588b1999-10-26 01:59:11 +000055
56long app_RAND_load_files(char *name)
Matt Caswell0f113f32015-01-22 03:40:55 +000057{
58 char *p, *n;
59 int last;
60 long tot = 0;
Benjamin Kaduk0423f812016-01-12 18:02:16 -060061#ifndef OPENSSL_NO_EGD
Matt Caswell0f113f32015-01-22 03:40:55 +000062 int egd;
Benjamin Kaduk0423f812016-01-12 18:02:16 -060063#endif
Bodo Möller640588b1999-10-26 01:59:11 +000064
Matt Caswell0f113f32015-01-22 03:40:55 +000065 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
Benjamin Kaduk0423f812016-01-12 18:02:16 -060076#ifndef OPENSSL_NO_EGD
Matt Caswell0f113f32015-01-22 03:40:55 +000077 egd = RAND_egd(n);
78 if (egd > 0)
79 tot += egd;
80 else
Benjamin Kaduk0423f812016-01-12 18:02:16 -060081#endif
Matt Caswell0f113f32015-01-22 03:40:55 +000082 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}
Bodo Möller640588b1999-10-26 01:59:11 +000090
Rich Salz7e1b7482015-04-24 15:26:15 -040091int app_RAND_write_file(const char *file)
Matt Caswell0f113f32015-01-22 03:40:55 +000092{
93 char buffer[200];
Bodo Möller640588b1999-10-26 01:59:11 +000094
Matt Caswell0f113f32015-01-22 03:40:55 +000095 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)) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400106 BIO_printf(bio_err, "unable to write 'random state'\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000107 return 0;
108 }
109 return 1;
110}
Bodo Möller640588b1999-10-26 01:59:11 +0000111
112void app_RAND_allow_write_file(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000113{
114 seeded = 1;
115}