]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/lib/app_rand.c
Fetch cipher after loading providers
[thirdparty/openssl.git] / apps / lib / app_rand.c
CommitLineData
846e33c7 1/*
c486283c 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
640588bb 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
640588bb
BM
8 */
9
a0ad17bb 10#include "apps.h"
640588bb 11#include <openssl/bio.h>
f1b8b001 12#include <openssl/err.h>
640588bb 13#include <openssl/rand.h>
3ee1eac2 14#include <openssl/conf.h>
640588bb 15
54e5ba05 16static char *save_rand_file;
03bbd346 17static char *files_to_load;
640588bb 18
3ee1eac2 19void app_RAND_load_conf(CONF *c, const char *section)
0f113f3e 20{
3ee1eac2 21 const char *randfile = NCONF_get_string(c, section, "RANDFILE");
0f113f3e 22
3ee1eac2
RS
23 if (randfile == NULL) {
24 ERR_clear_error();
25 return;
2234212c 26 }
3ee1eac2
RS
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);
0f113f3e 30 }
3ee1eac2 31 if (save_rand_file == NULL)
54e5ba05 32 save_rand_file = OPENSSL_strdup(randfile);
0f113f3e 33}
640588bb 34
51e5df0e 35int app_RAND_load(void)
0f113f3e 36{
03bbd346 37 char *p, *save;
3ee1eac2 38 int last, ret = 1;
640588bb 39
03bbd346 40 if (files_to_load == NULL)
51e5df0e
RS
41 return 1;
42
03bbd346 43 save = files_to_load;
3ee1eac2 44 for ( ; ; ) {
0f113f3e 45 last = 0;
03bbd346 46 for (p = files_to_load; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
3ee1eac2 47 continue;
0f113f3e
MC
48 if (*p == '\0')
49 last = 1;
50 *p = '\0';
03bbd346
RS
51 if (RAND_load_file(files_to_load, -1) < 0) {
52 BIO_printf(bio_err, "Can't load %s into RNG\n", files_to_load);
3ee1eac2
RS
53 ERR_print_errors(bio_err);
54 ret = 0;
55 }
0f113f3e
MC
56 if (last)
57 break;
03bbd346
RS
58 files_to_load = p + 1;
59 if (*files_to_load == '\0')
3ee1eac2 60 break;
0f113f3e 61 }
03bbd346
RS
62 files_to_load = NULL;
63 OPENSSL_free(save);
3ee1eac2 64 return ret;
0f113f3e 65}
640588bb 66
3ee1eac2 67void app_RAND_write(void)
0f113f3e 68{
3ee1eac2
RS
69 if (save_rand_file == NULL)
70 return;
71 if (RAND_write_file(save_rand_file) == -1) {
72 BIO_printf(bio_err, "Cannot write random bytes:\n");
73 ERR_print_errors(bio_err);
f367ac2b 74 }
54e5ba05
RS
75 OPENSSL_free(save_rand_file);
76 save_rand_file = NULL;
0f113f3e 77}
640588bb 78
3ee1eac2
RS
79
80/*
81 * See comments in opt_verify for explanation of this.
82 */
83enum r_range { OPT_R_ENUM };
84
85int opt_rand(int opt)
0f113f3e 86{
3ee1eac2
RS
87 switch ((enum r_range)opt) {
88 case OPT_R__FIRST:
89 case OPT_R__LAST:
90 break;
91 case OPT_R_RAND:
03bbd346 92 files_to_load = opt_arg();
3ee1eac2
RS
93 break;
94 case OPT_R_WRITERAND:
54e5ba05
RS
95 OPENSSL_free(save_rand_file);
96 save_rand_file = OPENSSL_strdup(opt_arg());
3ee1eac2
RS
97 break;
98 }
99 return 1;
0f113f3e 100}