2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include <openssl/bio.h>
12 #include <openssl/err.h>
13 #include <openssl/rand.h>
14 #include <openssl/conf.h>
16 static char *save_rand_file;
17 static STACK_OF(OPENSSL_STRING) *randfiles;
19 void app_RAND_load_conf(CONF *c, const char *section)
21 const char *randfile = NCONF_get_string(c, section, "RANDFILE");
23 if (randfile == NULL) {
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);
31 if (save_rand_file == NULL)
32 save_rand_file = OPENSSL_strdup(randfile);
35 static int loadfiles(char *name)
42 for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
47 if (RAND_load_file(name, -1) < 0) {
48 BIO_printf(bio_err, "Can't load %s into RNG\n", name);
49 ERR_print_errors(bio_err);
61 int app_RAND_load(void)
66 if (randfiles == NULL)
69 for (i = 0; i < sk_OPENSSL_STRING_num(randfiles); i++) {
70 p = sk_OPENSSL_STRING_value(randfiles, i);
74 sk_OPENSSL_STRING_free(randfiles);
78 void app_RAND_write(void)
80 if (save_rand_file == NULL)
82 if (RAND_write_file(save_rand_file) == -1) {
83 BIO_printf(bio_err, "Cannot write random bytes:\n");
84 ERR_print_errors(bio_err);
86 OPENSSL_free(save_rand_file);
87 save_rand_file = NULL;
92 * See comments in opt_verify for explanation of this.
94 enum r_range { OPT_R_ENUM };
98 switch ((enum r_range)opt) {
103 if (randfiles == NULL
104 && (randfiles = sk_OPENSSL_STRING_new_null()) == NULL)
106 if (!sk_OPENSSL_STRING_push(randfiles, opt_arg()))
109 case OPT_R_WRITERAND:
110 OPENSSL_free(save_rand_file);
111 save_rand_file = OPENSSL_strdup(opt_arg());