]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/lib/app_rand.c
2 * Copyright 1995-2021 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 for (i
= 0; i
< sk_OPENSSL_STRING_num(randfiles
); i
++) {
67 p
= sk_OPENSSL_STRING_value(randfiles
, i
);
71 sk_OPENSSL_STRING_free(randfiles
);
75 int app_RAND_write(void)
79 if (save_rand_file
== NULL
)
81 if (RAND_write_file(save_rand_file
) == -1) {
82 BIO_printf(bio_err
, "Cannot write random bytes:\n");
83 ERR_print_errors(bio_err
);
86 OPENSSL_free(save_rand_file
);
87 save_rand_file
= NULL
;
93 * See comments in opt_verify for explanation of this.
95 enum r_range
{ OPT_R_ENUM
};
99 switch ((enum r_range
)opt
) {
104 if (randfiles
== NULL
105 && (randfiles
= sk_OPENSSL_STRING_new_null()) == NULL
)
107 if (!sk_OPENSSL_STRING_push(randfiles
, opt_arg()))
110 case OPT_R_WRITERAND
:
111 OPENSSL_free(save_rand_file
);
112 save_rand_file
= OPENSSL_strdup(opt_arg());