freebsd-nq/crypto/openssl/apps/app_rand.c

94 lines
2.2 KiB
C
Raw Normal View History

2018-09-13 19:18:07 +00:00
/*
2018-11-20 18:59:41 +00:00
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
2000-04-13 06:33:22 +00:00
*
2018-09-13 19:18:07 +00:00
* Licensed under the OpenSSL license (the "License"). You may not use
* 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
2000-04-13 06:33:22 +00:00
*/
#include "apps.h"
#include <openssl/bio.h>
2018-09-13 19:18:07 +00:00
#include <openssl/err.h>
2000-04-13 06:33:22 +00:00
#include <openssl/rand.h>
2018-09-13 19:18:07 +00:00
#include <openssl/conf.h>
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
static char *save_rand_file;
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
void app_RAND_load_conf(CONF *c, const char *section)
2015-03-20 15:28:40 +00:00
{
2018-09-13 19:18:07 +00:00
const char *randfile = NCONF_get_string(c, section, "RANDFILE");
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
if (randfile == NULL) {
ERR_clear_error();
return;
2015-03-20 15:28:40 +00:00
}
2018-09-13 19:18:07 +00: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-03-20 15:28:40 +00:00
}
2018-09-13 19:18:07 +00:00
if (save_rand_file == NULL)
save_rand_file = OPENSSL_strdup(randfile);
2015-03-20 15:28:40 +00:00
}
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
static int loadfiles(char *name)
2015-03-20 15:28:40 +00:00
{
2018-09-13 19:18:07 +00:00
char *p;
int last, ret = 1;
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
for ( ; ; ) {
2015-03-20 15:28:40 +00:00
last = 0;
2018-09-13 19:18:07 +00:00
for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
continue;
2015-03-20 15:28:40 +00:00
if (*p == '\0')
last = 1;
*p = '\0';
2018-09-13 19:18:07 +00: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-03-20 15:28:40 +00:00
if (last)
break;
2018-09-13 19:18:07 +00:00
name = p + 1;
if (*name == '\0')
break;
2015-03-20 15:28:40 +00:00
}
2018-09-13 19:18:07 +00:00
return ret;
2015-03-20 15:28:40 +00:00
}
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
void app_RAND_write(void)
2015-03-20 15:28:40 +00:00
{
2018-09-13 19:18:07 +00: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);
2015-03-20 15:28:40 +00:00
}
2018-09-13 19:18:07 +00:00
OPENSSL_free(save_rand_file);
save_rand_file = NULL;
2015-03-20 15:28:40 +00:00
}
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
/*
* See comments in opt_verify for explanation of this.
*/
enum r_range { OPT_R_ENUM };
int opt_rand(int opt)
2015-03-20 15:28:40 +00:00
{
2018-09-13 19:18:07 +00: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:
OPENSSL_free(save_rand_file);
save_rand_file = OPENSSL_strdup(opt_arg());
break;
}
return 1;
2015-03-20 15:28:40 +00:00
}