2016-05-18 02:18:30 +08:00
|
|
|
/*
|
2018-11-20 21:13:00 +08:00
|
|
|
* Copyright 1995-2018 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;
|
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
|
|
|
{
|
2017-07-05 22:58:48 +08:00
|
|
|
const char *randfile = NCONF_get_string(c, section, "RANDFILE");
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-07-05 22:58:48 +08:00
|
|
|
if (randfile == NULL) {
|
|
|
|
ERR_clear_error();
|
|
|
|
return;
|
2017-06-13 01:24:02 +08:00
|
|
|
}
|
2017-07-05 22:58:48 +08:00
|
|
|
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
|
|
|
}
|
2017-07-05 22:58:48 +08:00
|
|
|
if (save_rand_file == NULL)
|
2017-07-17 14:52:26 +08:00
|
|
|
save_rand_file = OPENSSL_strdup(randfile);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2017-07-05 22:58:48 +08:00
|
|
|
static int loadfiles(char *name)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-07-17 09:05:13 +08:00
|
|
|
char *p;
|
2017-07-05 22:58:48 +08:00
|
|
|
int last, ret = 1;
|
1999-10-26 09:59:11 +08:00
|
|
|
|
2017-07-05 22:58:48 +08:00
|
|
|
for ( ; ; ) {
|
2015-01-22 11:40:55 +08:00
|
|
|
last = 0;
|
2017-07-05 22:58:48 +08:00
|
|
|
for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
|
|
|
|
continue;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (*p == '\0')
|
|
|
|
last = 1;
|
|
|
|
*p = '\0';
|
2017-07-05 22:58:48 +08:00
|
|
|
if (RAND_load_file(name, -1) < 0) {
|
|
|
|
BIO_printf(bio_err, "Can't load %s into RNG\n", name);
|
|
|
|
ERR_print_errors(bio_err);
|
|
|
|
ret = 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
if (last)
|
|
|
|
break;
|
2017-07-05 22:58:48 +08:00
|
|
|
name = p + 1;
|
|
|
|
if (*name == '\0')
|
|
|
|
break;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
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
|
|
|
|
2017-07-05 22:58:48 +08:00
|
|
|
void app_RAND_write(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-07-05 22:58:48 +08:00
|
|
|
if (save_rand_file == NULL)
|
|
|
|
return;
|
|
|
|
if (RAND_write_file(save_rand_file) == -1) {
|
|
|
|
BIO_printf(bio_err, "Cannot write random bytes:\n");
|
|
|
|
ERR_print_errors(bio_err);
|
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;
|
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:
|
|
|
|
return loadfiles(opt_arg());
|
|
|
|
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());
|
2017-07-05 22:58:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|