]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/lib/app_rand.c
fix some code with obvious wrong coding style
[thirdparty/openssl.git] / apps / lib / app_rand.c
CommitLineData
846e33c7 1/*
a28d06f3 2 * Copyright 1995-2021 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;
22040fb7 17static STACK_OF(OPENSSL_STRING) *randfiles;
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
22040fb7 35static int loadfiles(char *name)
0f113f3e 36{
22040fb7 37 char *p;
3ee1eac2 38 int last, ret = 1;
640588bb 39
1287dabd 40 for (;;) {
0f113f3e 41 last = 0;
22040fb7 42 for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
3ee1eac2 43 continue;
0f113f3e
MC
44 if (*p == '\0')
45 last = 1;
46 *p = '\0';
22040fb7
RS
47 if (RAND_load_file(name, -1) < 0) {
48 BIO_printf(bio_err, "Can't load %s into RNG\n", name);
3ee1eac2
RS
49 ERR_print_errors(bio_err);
50 ret = 0;
51 }
0f113f3e
MC
52 if (last)
53 break;
22040fb7
RS
54 name = p + 1;
55 if (*name == '\0')
3ee1eac2 56 break;
0f113f3e 57 }
22040fb7
RS
58 return ret;
59}
60
61int app_RAND_load(void)
62{
63 char *p;
64 int i, ret = 1;
65
22040fb7
RS
66 for (i = 0; i < sk_OPENSSL_STRING_num(randfiles); i++) {
67 p = sk_OPENSSL_STRING_value(randfiles, i);
68 if (!loadfiles(p))
69 ret = 0;
70 }
71 sk_OPENSSL_STRING_free(randfiles);
3ee1eac2 72 return ret;
0f113f3e 73}
640588bb 74
3ad60309 75int app_RAND_write(void)
0f113f3e 76{
3ad60309
DDO
77 int ret = 1;
78
3ee1eac2 79 if (save_rand_file == NULL)
3ad60309 80 return 1;
3ee1eac2
RS
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);
3ad60309 84 ret = 0;
f367ac2b 85 }
54e5ba05
RS
86 OPENSSL_free(save_rand_file);
87 save_rand_file = NULL;
3ad60309 88 return ret;
0f113f3e 89}
640588bb 90
3ee1eac2
RS
91
92/*
93 * See comments in opt_verify for explanation of this.
94 */
95enum r_range { OPT_R_ENUM };
96
97int opt_rand(int opt)
0f113f3e 98{
3ee1eac2
RS
99 switch ((enum r_range)opt) {
100 case OPT_R__FIRST:
101 case OPT_R__LAST:
102 break;
103 case OPT_R_RAND:
22040fb7
RS
104 if (randfiles == NULL
105 && (randfiles = sk_OPENSSL_STRING_new_null()) == NULL)
106 return 0;
107 if (!sk_OPENSSL_STRING_push(randfiles, opt_arg()))
108 return 0;
3ee1eac2
RS
109 break;
110 case OPT_R_WRITERAND:
54e5ba05
RS
111 OPENSSL_free(save_rand_file);
112 save_rand_file = OPENSSL_strdup(opt_arg());
3ee1eac2
RS
113 break;
114 }
115 return 1;
0f113f3e 116}