Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 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 |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Dr. Stephen Henson | a0ad17b | 1999-11-08 13:58:08 +0000 | [diff] [blame] | 10 | #include "apps.h" |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 11 | #include <openssl/bio.h> |
| 12 | #include <openssl/rand.h> |
| 13 | |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 14 | static int seeded = 0; |
Ulf Möller | 4ec2d4d | 2000-02-24 02:51:47 +0000 | [diff] [blame] | 15 | static int egdsocket = 0; |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 16 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 17 | int app_RAND_load_file(const char *file, int dont_warn) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 18 | { |
| 19 | int consider_randfile = (file == NULL); |
| 20 | char buffer[200]; |
| 21 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 22 | if (file == NULL) |
| 23 | file = RAND_file_name(buffer, sizeof buffer); |
Benjamin Kaduk | 0423f81 | 2016-01-12 18:02:16 -0600 | [diff] [blame] | 24 | #ifndef OPENSSL_NO_EGD |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 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 | } |
Benjamin Kaduk | 0423f81 | 2016-01-12 18:02:16 -0600 | [diff] [blame] | 33 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 34 | if (file == NULL || !RAND_load_file(file, -1)) { |
| 35 | if (RAND_status() == 0) { |
| 36 | if (!dont_warn) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 37 | BIO_printf(bio_err, "unable to load 'random state'\n"); |
| 38 | BIO_printf(bio_err, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 39 | "This means that the random number generator has not been seeded\n"); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 40 | BIO_printf(bio_err, "with much random data.\n"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 41 | if (consider_randfile) { /* explanation does not apply when a |
| 42 | * file is explicitly named */ |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 43 | BIO_printf(bio_err, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 44 | "Consider setting the RANDFILE environment variable to point at a file that\n"); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 45 | BIO_printf(bio_err, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 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 | } |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 55 | |
| 56 | long app_RAND_load_files(char *name) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 57 | { |
| 58 | char *p, *n; |
| 59 | int last; |
| 60 | long tot = 0; |
Benjamin Kaduk | 0423f81 | 2016-01-12 18:02:16 -0600 | [diff] [blame] | 61 | #ifndef OPENSSL_NO_EGD |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 62 | int egd; |
Benjamin Kaduk | 0423f81 | 2016-01-12 18:02:16 -0600 | [diff] [blame] | 63 | #endif |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 64 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 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 | |
Benjamin Kaduk | 0423f81 | 2016-01-12 18:02:16 -0600 | [diff] [blame] | 76 | #ifndef OPENSSL_NO_EGD |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 77 | egd = RAND_egd(n); |
| 78 | if (egd > 0) |
| 79 | tot += egd; |
| 80 | else |
Benjamin Kaduk | 0423f81 | 2016-01-12 18:02:16 -0600 | [diff] [blame] | 81 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 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 | } |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 90 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 91 | int app_RAND_write_file(const char *file) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 92 | { |
| 93 | char buffer[200]; |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 94 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 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)) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 106 | BIO_printf(bio_err, "unable to write 'random state'\n"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | return 1; |
| 110 | } |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 111 | |
| 112 | void app_RAND_allow_write_file(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 113 | { |
| 114 | seeded = 1; |
| 115 | } |