VirtualBox

source: vbox/trunk/src/libs/openssl-3.4.1/apps/lib/app_rand.c@ 109302

Last change on this file since 109302 was 109052, checked in by vboxsync, 4 weeks ago

openssl-3.4.1: Applied our changes, regenerated files, added missing files and functions. This time with a three way merge. ​bugref:10890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 KB
Line 
1/*
2 * Copyright 1995-2023 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
16static char *save_rand_file;
17static STACK_OF(OPENSSL_STRING) *randfiles;
18
19void app_RAND_load_conf(CONF *c, const char *section)
20{
21 const char *randfile = app_conf_try_string(c, section, "RANDFILE");
22
23 if (randfile == NULL)
24 return;
25 if (RAND_load_file(randfile, -1) < 0) {
26 BIO_printf(bio_err, "Can't load %s into RNG\n", randfile);
27 ERR_print_errors(bio_err);
28 }
29 if (save_rand_file == NULL) {
30 save_rand_file = OPENSSL_strdup(randfile);
31 /* If some internal memory errors have occurred */
32 if (save_rand_file == NULL) {
33 BIO_printf(bio_err, "Can't duplicate %s\n", randfile);
34 ERR_print_errors(bio_err);
35 }
36 }
37}
38
39static int loadfiles(char *name)
40{
41 char *p;
42 int last, ret = 1;
43
44 for (;;) {
45 last = 0;
46 for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
47 continue;
48 if (*p == '\0')
49 last = 1;
50 *p = '\0';
51 if (RAND_load_file(name, -1) < 0) {
52 BIO_printf(bio_err, "Can't load %s into RNG\n", name);
53 ERR_print_errors(bio_err);
54 ret = 0;
55 }
56 if (last)
57 break;
58 name = p + 1;
59 if (*name == '\0')
60 break;
61 }
62 return ret;
63}
64
65int app_RAND_load(void)
66{
67 char *p;
68 int i, ret = 1;
69
70 for (i = 0; i < sk_OPENSSL_STRING_num(randfiles); i++) {
71 p = sk_OPENSSL_STRING_value(randfiles, i);
72 if (!loadfiles(p))
73 ret = 0;
74 }
75 sk_OPENSSL_STRING_free(randfiles);
76 return ret;
77}
78
79int app_RAND_write(void)
80{
81 int ret = 1;
82
83 if (save_rand_file == NULL)
84 return 1;
85 if (RAND_write_file(save_rand_file) == -1) {
86 BIO_printf(bio_err, "Cannot write random bytes:\n");
87 ERR_print_errors(bio_err);
88 ret = 0;
89 }
90 OPENSSL_free(save_rand_file);
91 save_rand_file = NULL;
92 return ret;
93}
94
95
96/*
97 * See comments in opt_verify for explanation of this.
98 */
99enum r_range { OPT_R_ENUM };
100
101int opt_rand(int opt)
102{
103 switch ((enum r_range)opt) {
104 case OPT_R__FIRST:
105 case OPT_R__LAST:
106 break;
107 case OPT_R_RAND:
108 if (randfiles == NULL
109 && (randfiles = sk_OPENSSL_STRING_new_null()) == NULL)
110 return 0;
111 if (!sk_OPENSSL_STRING_push(randfiles, opt_arg()))
112 return 0;
113 break;
114 case OPT_R_WRITERAND:
115 OPENSSL_free(save_rand_file);
116 save_rand_file = OPENSSL_strdup(opt_arg());
117 if (save_rand_file == NULL)
118 return 0;
119 break;
120 }
121 return 1;
122}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette