2016-05-18 02:18:30 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1999-10-26 09:59:11 +08:00
|
|
|
*
|
2018-12-06 20:00:26 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:18:30 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-10-26 09:59:11 +08:00
|
|
|
*/
|
|
|
|
|
1999-11-08 21:58:08 +08:00
|
|
|
#include "apps.h"
|
1999-10-26 09:59:11 +08:00
|
|
|
#include <openssl/bio.h>
|
2017-07-17 09:05:13 +08:00
|
|
|
#include <openssl/err.h>
|
1999-10-26 09:59:11 +08:00
|
|
|
#include <openssl/rand.h>
|
2017-07-05 22:58:48 +08:00
|
|
|
#include <openssl/conf.h>
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2017-07-17 14:52:26 +08:00
|
|
|
static char *save_rand_file;
|
2021-02-11 02:33:41 +08:00
|
|
|
static STACK_OF(OPENSSL_STRING) *randfiles;
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2017-07-05 22:58:48 +08:00
|
|
|
void app_RAND_load_conf(CONF *c, const char *section)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2023-05-16 16:17:03 +08:00
|
|
|
const char *randfile = app_conf_try_string(c, section, "RANDFILE");
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2023-05-16 16:17:03 +08:00
|
|
|
if (randfile == NULL)
|
2017-07-05 22:58:48 +08:00
|
|
|
return;
|
|
|
|
if (RAND_load_file(randfile, -1) < 0) {
|
|
|
|
BIO_printf(bio_err, "Can't load %s into RNG\n", randfile);
|
|
|
|
ERR_print_errors(bio_err);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-11-24 11:26:09 +08:00
|
|
|
if (save_rand_file == NULL) {
|
2017-07-17 14:52:26 +08:00
|
|
|
save_rand_file = OPENSSL_strdup(randfile);
|
2021-11-24 11:26:09 +08:00
|
|
|
/* If some internal memory errors have occurred */
|
|
|
|
if (save_rand_file == NULL) {
|
|
|
|
BIO_printf(bio_err, "Can't duplicate %s\n", randfile);
|
|
|
|
ERR_print_errors(bio_err);
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2021-02-11 02:33:41 +08:00
|
|
|
static int loadfiles(char *name)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2021-02-11 02:33:41 +08:00
|
|
|
char *p;
|
2017-07-05 22:58:48 +08:00
|
|
|
int last, ret = 1;
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2021-10-26 15:16:18 +08:00
|
|
|
for (;;) {
|
2015-01-22 11:40:55 +08:00
|
|
|
last = 0;
|
2021-02-11 02:33:41 +08:00
|
|
|
for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
|
2017-07-05 22:58:48 +08:00
|
|
|
continue;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (*p == '\0')
|
|
|
|
last = 1;
|
|
|
|
*p = '\0';
|
2021-02-11 02:33:41 +08:00
|
|
|
if (RAND_load_file(name, -1) < 0) {
|
|
|
|
BIO_printf(bio_err, "Can't load %s into RNG\n", name);
|
2017-07-05 22:58:48 +08:00
|
|
|
ERR_print_errors(bio_err);
|
|
|
|
ret = 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
if (last)
|
|
|
|
break;
|
2021-02-11 02:33:41 +08:00
|
|
|
name = p + 1;
|
|
|
|
if (*name == '\0')
|
2017-07-05 22:58:48 +08:00
|
|
|
break;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-02-11 02:33:41 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int app_RAND_load(void)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int i, ret = 1;
|
|
|
|
|
|
|
|
for (i = 0; i < sk_OPENSSL_STRING_num(randfiles); i++) {
|
|
|
|
p = sk_OPENSSL_STRING_value(randfiles, i);
|
|
|
|
if (!loadfiles(p))
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
sk_OPENSSL_STRING_free(randfiles);
|
2017-07-05 22:58:48 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2021-04-03 18:53:51 +08:00
|
|
|
int app_RAND_write(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2021-04-03 18:53:51 +08:00
|
|
|
int ret = 1;
|
|
|
|
|
2017-07-05 22:58:48 +08:00
|
|
|
if (save_rand_file == NULL)
|
2021-04-03 18:53:51 +08:00
|
|
|
return 1;
|
2017-07-05 22:58:48 +08:00
|
|
|
if (RAND_write_file(save_rand_file) == -1) {
|
|
|
|
BIO_printf(bio_err, "Cannot write random bytes:\n");
|
|
|
|
ERR_print_errors(bio_err);
|
2021-04-03 18:53:51 +08:00
|
|
|
ret = 0;
|
2017-06-27 00:02:57 +08:00
|
|
|
}
|
2017-07-17 14:52:26 +08:00
|
|
|
OPENSSL_free(save_rand_file);
|
|
|
|
save_rand_file = NULL;
|
2021-04-03 18:53:51 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2017-07-05 22:58:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See comments in opt_verify for explanation of this.
|
|
|
|
*/
|
|
|
|
enum r_range { OPT_R_ENUM };
|
|
|
|
|
|
|
|
int opt_rand(int opt)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-07-05 22:58:48 +08:00
|
|
|
switch ((enum r_range)opt) {
|
|
|
|
case OPT_R__FIRST:
|
|
|
|
case OPT_R__LAST:
|
|
|
|
break;
|
|
|
|
case OPT_R_RAND:
|
2021-02-11 02:33:41 +08:00
|
|
|
if (randfiles == NULL
|
|
|
|
&& (randfiles = sk_OPENSSL_STRING_new_null()) == NULL)
|
|
|
|
return 0;
|
|
|
|
if (!sk_OPENSSL_STRING_push(randfiles, opt_arg()))
|
|
|
|
return 0;
|
2017-07-05 22:58:48 +08:00
|
|
|
break;
|
|
|
|
case OPT_R_WRITERAND:
|
2017-07-17 14:52:26 +08:00
|
|
|
OPENSSL_free(save_rand_file);
|
|
|
|
save_rand_file = OPENSSL_strdup(opt_arg());
|
2021-11-23 21:33:17 +08:00
|
|
|
if (save_rand_file == NULL)
|
|
|
|
return 0;
|
2017-07-05 22:58:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|