]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/app_rand.c
OPENSSL_s390xcap.pod: list msa9 facility bit (155)
[thirdparty/openssl.git] / apps / 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;
640588bb 17
3ee1eac2 18void app_RAND_load_conf(CONF *c, const char *section)
0f113f3e 19{
3ee1eac2 20 const char *randfile = NCONF_get_string(c, section, "RANDFILE");
0f113f3e 21
3ee1eac2
RS
22 if (randfile == NULL) {
23 ERR_clear_error();
24 return;
2234212c 25 }
3ee1eac2
RS
26 if (RAND_load_file(randfile, -1) < 0) {
27 BIO_printf(bio_err, "Can't load %s into RNG\n", randfile);
28 ERR_print_errors(bio_err);
0f113f3e 29 }
3ee1eac2 30 if (save_rand_file == NULL)
54e5ba05 31 save_rand_file = OPENSSL_strdup(randfile);
0f113f3e 32}
640588bb 33
3ee1eac2 34static int loadfiles(char *name)
0f113f3e 35{
f1b8b001 36 char *p;
3ee1eac2 37 int last, ret = 1;
640588bb 38
3ee1eac2 39 for ( ; ; ) {
0f113f3e 40 last = 0;
3ee1eac2
RS
41 for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
42 continue;
0f113f3e
MC
43 if (*p == '\0')
44 last = 1;
45 *p = '\0';
3ee1eac2
RS
46 if (RAND_load_file(name, -1) < 0) {
47 BIO_printf(bio_err, "Can't load %s into RNG\n", name);
48 ERR_print_errors(bio_err);
49 ret = 0;
50 }
0f113f3e
MC
51 if (last)
52 break;
3ee1eac2
RS
53 name = p + 1;
54 if (*name == '\0')
55 break;
0f113f3e 56 }
3ee1eac2 57 return ret;
0f113f3e 58}
640588bb 59
3ee1eac2 60void app_RAND_write(void)
0f113f3e 61{
3ee1eac2
RS
62 if (save_rand_file == NULL)
63 return;
64 if (RAND_write_file(save_rand_file) == -1) {
65 BIO_printf(bio_err, "Cannot write random bytes:\n");
66 ERR_print_errors(bio_err);
f367ac2b 67 }
54e5ba05
RS
68 OPENSSL_free(save_rand_file);
69 save_rand_file = NULL;
0f113f3e 70}
640588bb 71
3ee1eac2
RS
72
73/*
74 * See comments in opt_verify for explanation of this.
75 */
76enum r_range { OPT_R_ENUM };
77
78int opt_rand(int opt)
0f113f3e 79{
3ee1eac2
RS
80 switch ((enum r_range)opt) {
81 case OPT_R__FIRST:
82 case OPT_R__LAST:
83 break;
84 case OPT_R_RAND:
85 return loadfiles(opt_arg());
86 break;
87 case OPT_R_WRITERAND:
54e5ba05
RS
88 OPENSSL_free(save_rand_file);
89 save_rand_file = OPENSSL_strdup(opt_arg());
3ee1eac2
RS
90 break;
91 }
92 return 1;
0f113f3e 93}