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