Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
Matt Caswell | c486283 | 2018-11-20 13:13:00 +0000 | [diff] [blame] | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 3 | * |
Richard Levitte | dffa752 | 2018-12-06 13:00:26 +0100 | [diff] [blame] | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 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> |
Pauli | f1b8b00 | 2017-07-17 11:05:13 +1000 | [diff] [blame] | 12 | #include <openssl/err.h> |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 13 | #include <openssl/rand.h> |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 14 | #include <openssl/conf.h> |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 15 | |
Rich Salz | 54e5ba0 | 2017-07-17 02:52:26 -0400 | [diff] [blame] | 16 | static char *save_rand_file; |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 17 | static char *files_to_load; |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 18 | |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 19 | void app_RAND_load_conf(CONF *c, const char *section) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 20 | { |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 21 | const char *randfile = NCONF_get_string(c, section, "RANDFILE"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 22 | |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 23 | if (randfile == NULL) { |
| 24 | ERR_clear_error(); |
| 25 | return; |
Paul Yang | 2234212 | 2017-06-13 01:24:02 +0800 | [diff] [blame] | 26 | } |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 27 | if (RAND_load_file(randfile, -1) < 0) { |
| 28 | BIO_printf(bio_err, "Can't load %s into RNG\n", randfile); |
| 29 | ERR_print_errors(bio_err); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 30 | } |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 31 | if (save_rand_file == NULL) |
Rich Salz | 54e5ba0 | 2017-07-17 02:52:26 -0400 | [diff] [blame] | 32 | save_rand_file = OPENSSL_strdup(randfile); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 33 | } |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 34 | |
Rich Salz | 51e5df0 | 2021-02-08 13:45:23 -0500 | [diff] [blame] | 35 | int app_RAND_load(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 36 | { |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 37 | char *p, *save; |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 38 | int last, ret = 1; |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 39 | |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 40 | if (files_to_load == NULL) |
Rich Salz | 51e5df0 | 2021-02-08 13:45:23 -0500 | [diff] [blame] | 41 | return 1; |
| 42 | |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 43 | save = files_to_load; |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 44 | for ( ; ; ) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 45 | last = 0; |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 46 | for (p = files_to_load; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++) |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 47 | continue; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 48 | if (*p == '\0') |
| 49 | last = 1; |
| 50 | *p = '\0'; |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 51 | if (RAND_load_file(files_to_load, -1) < 0) { |
| 52 | BIO_printf(bio_err, "Can't load %s into RNG\n", files_to_load); |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 53 | ERR_print_errors(bio_err); |
| 54 | ret = 0; |
| 55 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 56 | if (last) |
| 57 | break; |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 58 | files_to_load = p + 1; |
| 59 | if (*files_to_load == '\0') |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 60 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | } |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 62 | files_to_load = NULL; |
| 63 | OPENSSL_free(save); |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 64 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 65 | } |
Bodo Möller | 640588b | 1999-10-26 01:59:11 +0000 | [diff] [blame] | 66 | |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 67 | void app_RAND_write(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 68 | { |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 69 | if (save_rand_file == NULL) |
| 70 | return; |
| 71 | if (RAND_write_file(save_rand_file) == -1) { |
| 72 | BIO_printf(bio_err, "Cannot write random bytes:\n"); |
| 73 | ERR_print_errors(bio_err); |
Rich Salz | f367ac2 | 2017-06-26 12:02:57 -0400 | [diff] [blame] | 74 | } |
Rich Salz | 54e5ba0 | 2017-07-17 02:52:26 -0400 | [diff] [blame] | 75 | OPENSSL_free(save_rand_file); |
| 76 | save_rand_file = NULL; |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 77 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 78 | |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * See comments in opt_verify for explanation of this. |
| 82 | */ |
| 83 | enum r_range { OPT_R_ENUM }; |
| 84 | |
| 85 | int opt_rand(int opt) |
| 86 | { |
| 87 | switch ((enum r_range)opt) { |
| 88 | case OPT_R__FIRST: |
| 89 | case OPT_R__LAST: |
| 90 | break; |
| 91 | case OPT_R_RAND: |
Rich Salz | 03bbd34 | 2021-02-08 14:20:01 -0500 | [diff] [blame^] | 92 | files_to_load = opt_arg(); |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 93 | break; |
| 94 | case OPT_R_WRITERAND: |
Rich Salz | 54e5ba0 | 2017-07-17 02:52:26 -0400 | [diff] [blame] | 95 | OPENSSL_free(save_rand_file); |
| 96 | save_rand_file = OPENSSL_strdup(opt_arg()); |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 97 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 98 | } |
| 99 | return 1; |
| 100 | } |