diff options
Diffstat (limited to 'src/lib/libssl/src/apps/rand.c')
-rw-r--r-- | src/lib/libssl/src/apps/rand.c | 103 |
1 files changed, 42 insertions, 61 deletions
diff --git a/src/lib/libssl/src/apps/rand.c b/src/lib/libssl/src/apps/rand.c index c01df530ae..443a64d848 100644 --- a/src/lib/libssl/src/apps/rand.c +++ b/src/lib/libssl/src/apps/rand.c | |||
@@ -7,7 +7,7 @@ | |||
7 | * are met: | 7 | * are met: |
8 | * | 8 | * |
9 | * 1. Redistributions of source code must retain the above copyright | 9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. | 10 | * notice, this list of conditions and the following disclaimer. |
11 | * | 11 | * |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in | 13 | * notice, this list of conditions and the following disclaimer in |
@@ -75,8 +75,9 @@ | |||
75 | 75 | ||
76 | int MAIN(int, char **); | 76 | int MAIN(int, char **); |
77 | 77 | ||
78 | int MAIN(int argc, char **argv) | 78 | int |
79 | { | 79 | MAIN(int argc, char **argv) |
80 | { | ||
80 | int i, r, ret = 1; | 81 | int i, r, ret = 1; |
81 | int badopt; | 82 | int badopt; |
82 | char *outfile = NULL; | 83 | char *outfile = NULL; |
@@ -86,82 +87,68 @@ int MAIN(int argc, char **argv) | |||
86 | BIO *out = NULL; | 87 | BIO *out = NULL; |
87 | int num = -1; | 88 | int num = -1; |
88 | #ifndef OPENSSL_NO_ENGINE | 89 | #ifndef OPENSSL_NO_ENGINE |
89 | char *engine=NULL; | 90 | char *engine = NULL; |
90 | #endif | 91 | #endif |
91 | 92 | ||
92 | apps_startup(); | 93 | apps_startup(); |
93 | 94 | ||
94 | if (bio_err == NULL) | 95 | if (bio_err == NULL) |
95 | if ((bio_err = BIO_new(BIO_s_file())) != NULL) | 96 | if ((bio_err = BIO_new(BIO_s_file())) != NULL) |
96 | BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); | 97 | BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); |
97 | 98 | ||
98 | if (!load_config(bio_err, NULL)) | 99 | if (!load_config(bio_err, NULL)) |
99 | goto err; | 100 | goto err; |
100 | 101 | ||
101 | badopt = 0; | 102 | badopt = 0; |
102 | i = 0; | 103 | i = 0; |
103 | while (!badopt && argv[++i] != NULL) | 104 | while (!badopt && argv[++i] != NULL) { |
104 | { | 105 | if (strcmp(argv[i], "-out") == 0) { |
105 | if (strcmp(argv[i], "-out") == 0) | 106 | if ((argv[i + 1] != NULL) && (outfile == NULL)) |
106 | { | ||
107 | if ((argv[i+1] != NULL) && (outfile == NULL)) | ||
108 | outfile = argv[++i]; | 107 | outfile = argv[++i]; |
109 | else | 108 | else |
110 | badopt = 1; | 109 | badopt = 1; |
111 | } | 110 | } |
112 | #ifndef OPENSSL_NO_ENGINE | 111 | #ifndef OPENSSL_NO_ENGINE |
113 | else if (strcmp(argv[i], "-engine") == 0) | 112 | else if (strcmp(argv[i], "-engine") == 0) { |
114 | { | 113 | if ((argv[i + 1] != NULL) && (engine == NULL)) |
115 | if ((argv[i+1] != NULL) && (engine == NULL)) | ||
116 | engine = argv[++i]; | 114 | engine = argv[++i]; |
117 | else | 115 | else |
118 | badopt = 1; | 116 | badopt = 1; |
119 | } | 117 | } |
120 | #endif | 118 | #endif |
121 | else if (strcmp(argv[i], "-rand") == 0) | 119 | else if (strcmp(argv[i], "-rand") == 0) { |
122 | { | 120 | if ((argv[i + 1] != NULL) && (inrand == NULL)) |
123 | if ((argv[i+1] != NULL) && (inrand == NULL)) | ||
124 | inrand = argv[++i]; | 121 | inrand = argv[++i]; |
125 | else | 122 | else |
126 | badopt = 1; | 123 | badopt = 1; |
127 | } | 124 | } else if (strcmp(argv[i], "-base64") == 0) { |
128 | else if (strcmp(argv[i], "-base64") == 0) | ||
129 | { | ||
130 | if (!base64) | 125 | if (!base64) |
131 | base64 = 1; | 126 | base64 = 1; |
132 | else | 127 | else |
133 | badopt = 1; | 128 | badopt = 1; |
134 | } | 129 | } else if (strcmp(argv[i], "-hex") == 0) { |
135 | else if (strcmp(argv[i], "-hex") == 0) | ||
136 | { | ||
137 | if (!hex) | 130 | if (!hex) |
138 | hex = 1; | 131 | hex = 1; |
139 | else | 132 | else |
140 | badopt = 1; | 133 | badopt = 1; |
141 | } | 134 | } else if (isdigit((unsigned char) argv[i][0])) { |
142 | else if (isdigit((unsigned char)argv[i][0])) | 135 | if (num < 0) { |
143 | { | ||
144 | if (num < 0) | ||
145 | { | ||
146 | r = sscanf(argv[i], "%d", &num); | 136 | r = sscanf(argv[i], "%d", &num); |
147 | if (r == 0 || num < 0) | 137 | if (r == 0 || num < 0) |
148 | badopt = 1; | 138 | badopt = 1; |
149 | } | 139 | } else |
150 | else | ||
151 | badopt = 1; | 140 | badopt = 1; |
152 | } | 141 | } else |
153 | else | ||
154 | badopt = 1; | 142 | badopt = 1; |
155 | } | 143 | } |
156 | 144 | ||
157 | if (hex && base64) | 145 | if (hex && base64) |
158 | badopt = 1; | 146 | badopt = 1; |
159 | 147 | ||
160 | if (num < 0) | 148 | if (num < 0) |
161 | badopt = 1; | 149 | badopt = 1; |
162 | 150 | ||
163 | if (badopt) | 151 | if (badopt) { |
164 | { | ||
165 | BIO_printf(bio_err, "Usage: rand [options] num\n"); | 152 | BIO_printf(bio_err, "Usage: rand [options] num\n"); |
166 | BIO_printf(bio_err, "where options are\n"); | 153 | BIO_printf(bio_err, "where options are\n"); |
167 | BIO_printf(bio_err, "-out file - write to file\n"); | 154 | BIO_printf(bio_err, "-out file - write to file\n"); |
@@ -172,68 +159,62 @@ int MAIN(int argc, char **argv) | |||
172 | BIO_printf(bio_err, "-base64 - base64 encode output\n"); | 159 | BIO_printf(bio_err, "-base64 - base64 encode output\n"); |
173 | BIO_printf(bio_err, "-hex - hex encode output\n"); | 160 | BIO_printf(bio_err, "-hex - hex encode output\n"); |
174 | goto err; | 161 | goto err; |
175 | } | 162 | } |
176 | |||
177 | #ifndef OPENSSL_NO_ENGINE | 163 | #ifndef OPENSSL_NO_ENGINE |
178 | setup_engine(bio_err, engine, 0); | 164 | setup_engine(bio_err, engine, 0); |
179 | #endif | 165 | #endif |
180 | 166 | ||
181 | app_RAND_load_file(NULL, bio_err, (inrand != NULL)); | 167 | app_RAND_load_file(NULL, bio_err, (inrand != NULL)); |
182 | if (inrand != NULL) | 168 | if (inrand != NULL) |
183 | BIO_printf(bio_err,"%ld semi-random bytes loaded\n", | 169 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", |
184 | app_RAND_load_files(inrand)); | 170 | app_RAND_load_files(inrand)); |
185 | 171 | ||
186 | out = BIO_new(BIO_s_file()); | 172 | out = BIO_new(BIO_s_file()); |
187 | if (out == NULL) | 173 | if (out == NULL) |
188 | goto err; | 174 | goto err; |
189 | if (outfile != NULL) | 175 | if (outfile != NULL) |
190 | r = BIO_write_filename(out, outfile); | 176 | r = BIO_write_filename(out, outfile); |
191 | else | 177 | else { |
192 | { | ||
193 | r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); | 178 | r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); |
194 | } | 179 | } |
195 | if (r <= 0) | 180 | if (r <= 0) |
196 | goto err; | 181 | goto err; |
197 | 182 | ||
198 | if (base64) | 183 | if (base64) { |
199 | { | ||
200 | BIO *b64 = BIO_new(BIO_f_base64()); | 184 | BIO *b64 = BIO_new(BIO_f_base64()); |
201 | if (b64 == NULL) | 185 | if (b64 == NULL) |
202 | goto err; | 186 | goto err; |
203 | out = BIO_push(b64, out); | 187 | out = BIO_push(b64, out); |
204 | } | 188 | } |
205 | 189 | while (num > 0) { | |
206 | while (num > 0) | ||
207 | { | ||
208 | unsigned char buf[4096]; | 190 | unsigned char buf[4096]; |
209 | int chunk; | 191 | int chunk; |
210 | 192 | ||
211 | chunk = num; | 193 | chunk = num; |
212 | if (chunk > (int)sizeof(buf)) | 194 | if (chunk > (int) sizeof(buf)) |
213 | chunk = sizeof buf; | 195 | chunk = sizeof buf; |
214 | r = RAND_bytes(buf, chunk); | 196 | r = RAND_bytes(buf, chunk); |
215 | if (r <= 0) | 197 | if (r <= 0) |
216 | goto err; | 198 | goto err; |
217 | if (!hex) | 199 | if (!hex) |
218 | BIO_write(out, buf, chunk); | 200 | BIO_write(out, buf, chunk); |
219 | else | 201 | else { |
220 | { | ||
221 | for (i = 0; i < chunk; i++) | 202 | for (i = 0; i < chunk; i++) |
222 | BIO_printf(out, "%02x", buf[i]); | 203 | BIO_printf(out, "%02x", buf[i]); |
223 | } | ||
224 | num -= chunk; | ||
225 | } | 204 | } |
205 | num -= chunk; | ||
206 | } | ||
226 | if (hex) | 207 | if (hex) |
227 | BIO_puts(out, "\n"); | 208 | BIO_puts(out, "\n"); |
228 | (void)BIO_flush(out); | 209 | (void) BIO_flush(out); |
229 | 210 | ||
230 | app_RAND_write_file(NULL, bio_err); | 211 | app_RAND_write_file(NULL, bio_err); |
231 | ret = 0; | 212 | ret = 0; |
232 | 213 | ||
233 | err: | 214 | err: |
234 | ERR_print_errors(bio_err); | 215 | ERR_print_errors(bio_err); |
235 | if (out) | 216 | if (out) |
236 | BIO_free_all(out); | 217 | BIO_free_all(out); |
237 | apps_shutdown(); | 218 | apps_shutdown(); |
238 | return(ret); | 219 | return (ret); |
239 | } | 220 | } |