]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/lib/app_rand.c
Move libapps.a source to apps/lib
[thirdparty/openssl.git] / apps / lib / app_rand.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include "apps.h"
11 #include <openssl/bio.h>
12 #include <openssl/err.h>
13 #include <openssl/rand.h>
14 #include <openssl/conf.h>
15
16 static char *save_rand_file;
17
18 void app_RAND_load_conf(CONF *c, const char *section)
19 {
20 const char *randfile = NCONF_get_string(c, section, "RANDFILE");
21
22 if (randfile == NULL) {
23 ERR_clear_error();
24 return;
25 }
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);
29 }
30 if (save_rand_file == NULL)
31 save_rand_file = OPENSSL_strdup(randfile);
32 }
33
34 static int loadfiles(char *name)
35 {
36 char *p;
37 int last, ret = 1;
38
39 for ( ; ; ) {
40 last = 0;
41 for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
42 continue;
43 if (*p == '\0')
44 last = 1;
45 *p = '\0';
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 }
51 if (last)
52 break;
53 name = p + 1;
54 if (*name == '\0')
55 break;
56 }
57 return ret;
58 }
59
60 void app_RAND_write(void)
61 {
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);
67 }
68 OPENSSL_free(save_rand_file);
69 save_rand_file = NULL;
70 }
71
72
73 /*
74 * See comments in opt_verify for explanation of this.
75 */
76 enum r_range { OPT_R_ENUM };
77
78 int opt_rand(int opt)
79 {
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:
88 OPENSSL_free(save_rand_file);
89 save_rand_file = OPENSSL_strdup(opt_arg());
90 break;
91 }
92 return 1;
93 }