1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos #include "apps.h"
11*b0d17251Schristos #include <openssl/bio.h>
12*b0d17251Schristos #include <openssl/err.h>
13*b0d17251Schristos #include <openssl/rand.h>
14*b0d17251Schristos #include <openssl/conf.h>
15*b0d17251Schristos
16*b0d17251Schristos static char *save_rand_file;
STACK_OF(OPENSSL_STRING)17*b0d17251Schristos static STACK_OF(OPENSSL_STRING) *randfiles;
18*b0d17251Schristos
19*b0d17251Schristos void app_RAND_load_conf(CONF *c, const char *section)
20*b0d17251Schristos {
21*b0d17251Schristos const char *randfile = NCONF_get_string(c, section, "RANDFILE");
22*b0d17251Schristos
23*b0d17251Schristos if (randfile == NULL) {
24*b0d17251Schristos ERR_clear_error();
25*b0d17251Schristos return;
26*b0d17251Schristos }
27*b0d17251Schristos if (RAND_load_file(randfile, -1) < 0) {
28*b0d17251Schristos BIO_printf(bio_err, "Can't load %s into RNG\n", randfile);
29*b0d17251Schristos ERR_print_errors(bio_err);
30*b0d17251Schristos }
31*b0d17251Schristos if (save_rand_file == NULL) {
32*b0d17251Schristos save_rand_file = OPENSSL_strdup(randfile);
33*b0d17251Schristos /* If some internal memory errors have occurred */
34*b0d17251Schristos if (save_rand_file == NULL) {
35*b0d17251Schristos BIO_printf(bio_err, "Can't duplicate %s\n", randfile);
36*b0d17251Schristos ERR_print_errors(bio_err);
37*b0d17251Schristos }
38*b0d17251Schristos }
39*b0d17251Schristos }
40*b0d17251Schristos
loadfiles(char * name)41*b0d17251Schristos static int loadfiles(char *name)
42*b0d17251Schristos {
43*b0d17251Schristos char *p;
44*b0d17251Schristos int last, ret = 1;
45*b0d17251Schristos
46*b0d17251Schristos for ( ; ; ) {
47*b0d17251Schristos last = 0;
48*b0d17251Schristos for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
49*b0d17251Schristos continue;
50*b0d17251Schristos if (*p == '\0')
51*b0d17251Schristos last = 1;
52*b0d17251Schristos *p = '\0';
53*b0d17251Schristos if (RAND_load_file(name, -1) < 0) {
54*b0d17251Schristos BIO_printf(bio_err, "Can't load %s into RNG\n", name);
55*b0d17251Schristos ERR_print_errors(bio_err);
56*b0d17251Schristos ret = 0;
57*b0d17251Schristos }
58*b0d17251Schristos if (last)
59*b0d17251Schristos break;
60*b0d17251Schristos name = p + 1;
61*b0d17251Schristos if (*name == '\0')
62*b0d17251Schristos break;
63*b0d17251Schristos }
64*b0d17251Schristos return ret;
65*b0d17251Schristos }
66*b0d17251Schristos
app_RAND_load(void)67*b0d17251Schristos int app_RAND_load(void)
68*b0d17251Schristos {
69*b0d17251Schristos char *p;
70*b0d17251Schristos int i, ret = 1;
71*b0d17251Schristos
72*b0d17251Schristos for (i = 0; i < sk_OPENSSL_STRING_num(randfiles); i++) {
73*b0d17251Schristos p = sk_OPENSSL_STRING_value(randfiles, i);
74*b0d17251Schristos if (!loadfiles(p))
75*b0d17251Schristos ret = 0;
76*b0d17251Schristos }
77*b0d17251Schristos sk_OPENSSL_STRING_free(randfiles);
78*b0d17251Schristos return ret;
79*b0d17251Schristos }
80*b0d17251Schristos
app_RAND_write(void)81*b0d17251Schristos int app_RAND_write(void)
82*b0d17251Schristos {
83*b0d17251Schristos int ret = 1;
84*b0d17251Schristos
85*b0d17251Schristos if (save_rand_file == NULL)
86*b0d17251Schristos return 1;
87*b0d17251Schristos if (RAND_write_file(save_rand_file) == -1) {
88*b0d17251Schristos BIO_printf(bio_err, "Cannot write random bytes:\n");
89*b0d17251Schristos ERR_print_errors(bio_err);
90*b0d17251Schristos ret = 0;
91*b0d17251Schristos }
92*b0d17251Schristos OPENSSL_free(save_rand_file);
93*b0d17251Schristos save_rand_file = NULL;
94*b0d17251Schristos return ret;
95*b0d17251Schristos }
96*b0d17251Schristos
97*b0d17251Schristos
98*b0d17251Schristos /*
99*b0d17251Schristos * See comments in opt_verify for explanation of this.
100*b0d17251Schristos */
101*b0d17251Schristos enum r_range { OPT_R_ENUM };
102*b0d17251Schristos
opt_rand(int opt)103*b0d17251Schristos int opt_rand(int opt)
104*b0d17251Schristos {
105*b0d17251Schristos switch ((enum r_range)opt) {
106*b0d17251Schristos case OPT_R__FIRST:
107*b0d17251Schristos case OPT_R__LAST:
108*b0d17251Schristos break;
109*b0d17251Schristos case OPT_R_RAND:
110*b0d17251Schristos if (randfiles == NULL
111*b0d17251Schristos && (randfiles = sk_OPENSSL_STRING_new_null()) == NULL)
112*b0d17251Schristos return 0;
113*b0d17251Schristos if (!sk_OPENSSL_STRING_push(randfiles, opt_arg()))
114*b0d17251Schristos return 0;
115*b0d17251Schristos break;
116*b0d17251Schristos case OPT_R_WRITERAND:
117*b0d17251Schristos OPENSSL_free(save_rand_file);
118*b0d17251Schristos save_rand_file = OPENSSL_strdup(opt_arg());
119*b0d17251Schristos if (save_rand_file == NULL)
120*b0d17251Schristos return 0;
121*b0d17251Schristos break;
122*b0d17251Schristos }
123*b0d17251Schristos return 1;
124*b0d17251Schristos }
125