blob: b1c092752e0c5e96a9645abbe57e684e25edba13 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
Matt Caswellc4862832018-11-20 13:13:00 +00002 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
Bodo Möller640588b1999-10-26 01:59:11 +00003 *
Richard Levittedffa7522018-12-06 13:00:26 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salz846e33c2016-05-17 14:18:30 -04005 * 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>
Paulif1b8b002017-07-17 11:05:13 +100012#include <openssl/err.h>
Bodo Möller640588b1999-10-26 01:59:11 +000013#include <openssl/rand.h>
Rich Salz3ee1eac2017-07-05 10:58:48 -040014#include <openssl/conf.h>
Bodo Möller640588b1999-10-26 01:59:11 +000015
Rich Salz54e5ba02017-07-17 02:52:26 -040016static char *save_rand_file;
Rich Salz03bbd342021-02-08 14:20:01 -050017static char *files_to_load;
Bodo Möller640588b1999-10-26 01:59:11 +000018
Rich Salz3ee1eac2017-07-05 10:58:48 -040019void app_RAND_load_conf(CONF *c, const char *section)
Matt Caswell0f113f32015-01-22 03:40:55 +000020{
Rich Salz3ee1eac2017-07-05 10:58:48 -040021 const char *randfile = NCONF_get_string(c, section, "RANDFILE");
Matt Caswell0f113f32015-01-22 03:40:55 +000022
Rich Salz3ee1eac2017-07-05 10:58:48 -040023 if (randfile == NULL) {
24 ERR_clear_error();
25 return;
Paul Yang22342122017-06-13 01:24:02 +080026 }
Rich Salz3ee1eac2017-07-05 10:58:48 -040027 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 Caswell0f113f32015-01-22 03:40:55 +000030 }
Rich Salz3ee1eac2017-07-05 10:58:48 -040031 if (save_rand_file == NULL)
Rich Salz54e5ba02017-07-17 02:52:26 -040032 save_rand_file = OPENSSL_strdup(randfile);
Matt Caswell0f113f32015-01-22 03:40:55 +000033}
Bodo Möller640588b1999-10-26 01:59:11 +000034
Rich Salz51e5df02021-02-08 13:45:23 -050035int app_RAND_load(void)
Matt Caswell0f113f32015-01-22 03:40:55 +000036{
Rich Salz03bbd342021-02-08 14:20:01 -050037 char *p, *save;
Rich Salz3ee1eac2017-07-05 10:58:48 -040038 int last, ret = 1;
Bodo Möller640588b1999-10-26 01:59:11 +000039
Rich Salz03bbd342021-02-08 14:20:01 -050040 if (files_to_load == NULL)
Rich Salz51e5df02021-02-08 13:45:23 -050041 return 1;
42
Rich Salz03bbd342021-02-08 14:20:01 -050043 save = files_to_load;
Rich Salz3ee1eac2017-07-05 10:58:48 -040044 for ( ; ; ) {
Matt Caswell0f113f32015-01-22 03:40:55 +000045 last = 0;
Rich Salz03bbd342021-02-08 14:20:01 -050046 for (p = files_to_load; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
Rich Salz3ee1eac2017-07-05 10:58:48 -040047 continue;
Matt Caswell0f113f32015-01-22 03:40:55 +000048 if (*p == '\0')
49 last = 1;
50 *p = '\0';
Rich Salz03bbd342021-02-08 14:20:01 -050051 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 Salz3ee1eac2017-07-05 10:58:48 -040053 ERR_print_errors(bio_err);
54 ret = 0;
55 }
Matt Caswell0f113f32015-01-22 03:40:55 +000056 if (last)
57 break;
Rich Salz03bbd342021-02-08 14:20:01 -050058 files_to_load = p + 1;
59 if (*files_to_load == '\0')
Rich Salz3ee1eac2017-07-05 10:58:48 -040060 break;
Matt Caswell0f113f32015-01-22 03:40:55 +000061 }
Rich Salz03bbd342021-02-08 14:20:01 -050062 files_to_load = NULL;
63 OPENSSL_free(save);
Rich Salz3ee1eac2017-07-05 10:58:48 -040064 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +000065}
Bodo Möller640588b1999-10-26 01:59:11 +000066
Rich Salz3ee1eac2017-07-05 10:58:48 -040067void app_RAND_write(void)
Matt Caswell0f113f32015-01-22 03:40:55 +000068{
Rich Salz3ee1eac2017-07-05 10:58:48 -040069 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 Salzf367ac22017-06-26 12:02:57 -040074 }
Rich Salz54e5ba02017-07-17 02:52:26 -040075 OPENSSL_free(save_rand_file);
76 save_rand_file = NULL;
Rich Salz3ee1eac2017-07-05 10:58:48 -040077}
Matt Caswell0f113f32015-01-22 03:40:55 +000078
Rich Salz3ee1eac2017-07-05 10:58:48 -040079
80/*
81 * See comments in opt_verify for explanation of this.
82 */
83enum r_range { OPT_R_ENUM };
84
85int 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 Salz03bbd342021-02-08 14:20:01 -050092 files_to_load = opt_arg();
Rich Salz3ee1eac2017-07-05 10:58:48 -040093 break;
94 case OPT_R_WRITERAND:
Rich Salz54e5ba02017-07-17 02:52:26 -040095 OPENSSL_free(save_rand_file);
96 save_rand_file = OPENSSL_strdup(opt_arg());
Rich Salz3ee1eac2017-07-05 10:58:48 -040097 break;
Matt Caswell0f113f32015-01-22 03:40:55 +000098 }
99 return 1;
100}