sl@0
|
1 |
/* apps/rand.c */
|
sl@0
|
2 |
/* ====================================================================
|
sl@0
|
3 |
* Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
6 |
* modification, are permitted provided that the following conditions
|
sl@0
|
7 |
* are met:
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
10 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
13 |
* notice, this list of conditions and the following disclaimer in
|
sl@0
|
14 |
* the documentation and/or other materials provided with the
|
sl@0
|
15 |
* distribution.
|
sl@0
|
16 |
*
|
sl@0
|
17 |
* 3. All advertising materials mentioning features or use of this
|
sl@0
|
18 |
* software must display the following acknowledgment:
|
sl@0
|
19 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
20 |
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
sl@0
|
21 |
*
|
sl@0
|
22 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
sl@0
|
23 |
* endorse or promote products derived from this software without
|
sl@0
|
24 |
* prior written permission. For written permission, please contact
|
sl@0
|
25 |
* openssl-core@openssl.org.
|
sl@0
|
26 |
*
|
sl@0
|
27 |
* 5. Products derived from this software may not be called "OpenSSL"
|
sl@0
|
28 |
* nor may "OpenSSL" appear in their names without prior written
|
sl@0
|
29 |
* permission of the OpenSSL Project.
|
sl@0
|
30 |
*
|
sl@0
|
31 |
* 6. Redistributions of any form whatsoever must retain the following
|
sl@0
|
32 |
* acknowledgment:
|
sl@0
|
33 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
34 |
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
sl@0
|
35 |
*
|
sl@0
|
36 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
sl@0
|
37 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
38 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
sl@0
|
39 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
sl@0
|
40 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
sl@0
|
41 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
sl@0
|
42 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
sl@0
|
43 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
44 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
45 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
sl@0
|
46 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
sl@0
|
47 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
48 |
* ====================================================================
|
sl@0
|
49 |
*
|
sl@0
|
50 |
* This product includes cryptographic software written by Eric Young
|
sl@0
|
51 |
* (eay@cryptsoft.com). This product includes software written by Tim
|
sl@0
|
52 |
* Hudson (tjh@cryptsoft.com).
|
sl@0
|
53 |
*
|
sl@0
|
54 |
*/
|
sl@0
|
55 |
|
sl@0
|
56 |
#include "apps.h"
|
sl@0
|
57 |
|
sl@0
|
58 |
#include <ctype.h>
|
sl@0
|
59 |
#include <stdio.h>
|
sl@0
|
60 |
#include <string.h>
|
sl@0
|
61 |
|
sl@0
|
62 |
#include <openssl/bio.h>
|
sl@0
|
63 |
#include <openssl/err.h>
|
sl@0
|
64 |
#include <openssl/rand.h>
|
sl@0
|
65 |
|
sl@0
|
66 |
#undef PROG
|
sl@0
|
67 |
#define PROG rand_main
|
sl@0
|
68 |
|
sl@0
|
69 |
/* -out file - write to file
|
sl@0
|
70 |
* -rand file:file - PRNG seed files
|
sl@0
|
71 |
* -base64 - encode output
|
sl@0
|
72 |
* num - write 'num' bytes
|
sl@0
|
73 |
*/
|
sl@0
|
74 |
|
sl@0
|
75 |
|
sl@0
|
76 |
int MAIN(int, char **);
|
sl@0
|
77 |
|
sl@0
|
78 |
int MAIN(int argc, char **argv)
|
sl@0
|
79 |
{
|
sl@0
|
80 |
#ifndef OPENSSL_NO_ENGINE
|
sl@0
|
81 |
ENGINE *e = NULL;
|
sl@0
|
82 |
#endif
|
sl@0
|
83 |
int i, r, ret = 1;
|
sl@0
|
84 |
int badopt;
|
sl@0
|
85 |
char *outfile = NULL;
|
sl@0
|
86 |
char *inrand = NULL;
|
sl@0
|
87 |
int base64 = 0;
|
sl@0
|
88 |
BIO *out = NULL;
|
sl@0
|
89 |
int num = -1;
|
sl@0
|
90 |
#ifndef OPENSSL_NO_ENGINE
|
sl@0
|
91 |
char *engine=NULL;
|
sl@0
|
92 |
#endif
|
sl@0
|
93 |
|
sl@0
|
94 |
apps_startup();
|
sl@0
|
95 |
|
sl@0
|
96 |
if (bio_err == NULL)
|
sl@0
|
97 |
if ((bio_err = BIO_new(BIO_s_file())) != NULL)
|
sl@0
|
98 |
BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
|
sl@0
|
99 |
|
sl@0
|
100 |
|
sl@0
|
101 |
if (!load_config(bio_err, NULL))
|
sl@0
|
102 |
goto err;
|
sl@0
|
103 |
|
sl@0
|
104 |
badopt = 0;
|
sl@0
|
105 |
i = 0;
|
sl@0
|
106 |
while (!badopt && argv[++i] != NULL)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
if (strcmp(argv[i], "-out") == 0)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
if ((argv[i+1] != NULL) && (outfile == NULL))
|
sl@0
|
111 |
outfile = argv[++i];
|
sl@0
|
112 |
else
|
sl@0
|
113 |
badopt = 1;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
#ifndef OPENSSL_NO_ENGINE
|
sl@0
|
116 |
else if (strcmp(argv[i], "-engine") == 0)
|
sl@0
|
117 |
{
|
sl@0
|
118 |
if ((argv[i+1] != NULL) && (engine == NULL))
|
sl@0
|
119 |
engine = argv[++i];
|
sl@0
|
120 |
else
|
sl@0
|
121 |
badopt = 1;
|
sl@0
|
122 |
}
|
sl@0
|
123 |
#endif
|
sl@0
|
124 |
else if (strcmp(argv[i], "-rand") == 0)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
if ((argv[i+1] != NULL) && (inrand == NULL))
|
sl@0
|
127 |
inrand = argv[++i];
|
sl@0
|
128 |
else
|
sl@0
|
129 |
badopt = 1;
|
sl@0
|
130 |
}
|
sl@0
|
131 |
else if (strcmp(argv[i], "-base64") == 0)
|
sl@0
|
132 |
{
|
sl@0
|
133 |
if (!base64)
|
sl@0
|
134 |
base64 = 1;
|
sl@0
|
135 |
else
|
sl@0
|
136 |
badopt = 1;
|
sl@0
|
137 |
}
|
sl@0
|
138 |
else if (isdigit((unsigned char)argv[i][0]))
|
sl@0
|
139 |
{
|
sl@0
|
140 |
if (num < 0)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
r = sscanf(argv[i], "%d", &num);
|
sl@0
|
143 |
if (r == 0 || num < 0)
|
sl@0
|
144 |
badopt = 1;
|
sl@0
|
145 |
}
|
sl@0
|
146 |
else
|
sl@0
|
147 |
badopt = 1;
|
sl@0
|
148 |
}
|
sl@0
|
149 |
else
|
sl@0
|
150 |
badopt = 1;
|
sl@0
|
151 |
}
|
sl@0
|
152 |
|
sl@0
|
153 |
if (num < 0)
|
sl@0
|
154 |
badopt = 1;
|
sl@0
|
155 |
|
sl@0
|
156 |
if (badopt)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
BIO_printf(bio_err, "Usage: rand [options] num\n");
|
sl@0
|
159 |
BIO_printf(bio_err, "where options are\n");
|
sl@0
|
160 |
BIO_printf(bio_err, "-out file - write to file\n");
|
sl@0
|
161 |
#ifndef OPENSSL_NO_ENGINE
|
sl@0
|
162 |
BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
|
sl@0
|
163 |
#endif
|
sl@0
|
164 |
BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
|
sl@0
|
165 |
BIO_printf(bio_err, "-base64 - encode output\n");
|
sl@0
|
166 |
goto err;
|
sl@0
|
167 |
}
|
sl@0
|
168 |
|
sl@0
|
169 |
#ifndef OPENSSL_NO_ENGINE
|
sl@0
|
170 |
e = setup_engine(bio_err, engine, 0);
|
sl@0
|
171 |
#endif
|
sl@0
|
172 |
|
sl@0
|
173 |
app_RAND_load_file(NULL, bio_err, (inrand != NULL));
|
sl@0
|
174 |
if (inrand != NULL)
|
sl@0
|
175 |
BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
|
sl@0
|
176 |
app_RAND_load_files(inrand));
|
sl@0
|
177 |
|
sl@0
|
178 |
out = BIO_new(BIO_s_file());
|
sl@0
|
179 |
if (out == NULL)
|
sl@0
|
180 |
goto err;
|
sl@0
|
181 |
if (outfile != NULL)
|
sl@0
|
182 |
r = BIO_write_filename(out, outfile);
|
sl@0
|
183 |
else
|
sl@0
|
184 |
{
|
sl@0
|
185 |
r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
|
sl@0
|
186 |
|
sl@0
|
187 |
#ifdef OPENSSL_SYS_VMS
|
sl@0
|
188 |
{
|
sl@0
|
189 |
BIO *tmpbio = BIO_new(BIO_f_linebuffer());
|
sl@0
|
190 |
out = BIO_push(tmpbio, out);
|
sl@0
|
191 |
}
|
sl@0
|
192 |
#endif
|
sl@0
|
193 |
}
|
sl@0
|
194 |
if (r <= 0)
|
sl@0
|
195 |
goto err;
|
sl@0
|
196 |
|
sl@0
|
197 |
if (base64)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
BIO *b64 = BIO_new(BIO_f_base64());
|
sl@0
|
200 |
if (b64 == NULL)
|
sl@0
|
201 |
goto err;
|
sl@0
|
202 |
out = BIO_push(b64, out);
|
sl@0
|
203 |
}
|
sl@0
|
204 |
|
sl@0
|
205 |
while (num > 0)
|
sl@0
|
206 |
{
|
sl@0
|
207 |
unsigned char buf[4096];
|
sl@0
|
208 |
int chunk;
|
sl@0
|
209 |
|
sl@0
|
210 |
chunk = num;
|
sl@0
|
211 |
if (chunk > (int)sizeof(buf))
|
sl@0
|
212 |
chunk = sizeof buf;
|
sl@0
|
213 |
r = RAND_bytes(buf, chunk);
|
sl@0
|
214 |
if (r <= 0)
|
sl@0
|
215 |
goto err;
|
sl@0
|
216 |
BIO_write(out, buf, chunk);
|
sl@0
|
217 |
num -= chunk;
|
sl@0
|
218 |
}
|
sl@0
|
219 |
(void)BIO_flush(out);
|
sl@0
|
220 |
|
sl@0
|
221 |
app_RAND_write_file(NULL, bio_err);
|
sl@0
|
222 |
ret = 0;
|
sl@0
|
223 |
|
sl@0
|
224 |
err:
|
sl@0
|
225 |
ERR_print_errors(bio_err);
|
sl@0
|
226 |
if (out)
|
sl@0
|
227 |
BIO_free_all(out);
|
sl@0
|
228 |
apps_shutdown();
|
sl@0
|
229 |
OPENSSL_EXIT(ret);
|
sl@0
|
230 |
}
|