]>
Commit | Line | Data |
---|---|---|
27b78273 BM |
1 | /* apps/rand.c */ |
2 | ||
3 | #include "apps.h" | |
4 | ||
5 | #include <ctype.h> | |
6 | #include <stdio.h> | |
8824ec7c | 7 | #include <string.h> |
27b78273 BM |
8 | |
9 | #include <openssl/bio.h> | |
10 | #include <openssl/err.h> | |
11 | #include <openssl/rand.h> | |
5270e702 | 12 | #include <openssl/engine.h> |
27b78273 BM |
13 | |
14 | #undef PROG | |
15 | #define PROG rand_main | |
16 | ||
17 | /* -out file - write to file | |
18 | * -rand file:file - PRNG seed files | |
19 | * -base64 - encode output | |
20 | * num - write 'num' bytes | |
21 | */ | |
22 | ||
23 | int MAIN(int, char **); | |
24 | ||
25 | int MAIN(int argc, char **argv) | |
26 | { | |
5270e702 | 27 | ENGINE *e = NULL; |
27b78273 BM |
28 | int i, r, ret = 1; |
29 | int badopt; | |
30 | char *outfile = NULL; | |
f365611c | 31 | char *inrand = NULL; |
27b78273 BM |
32 | int base64 = 0; |
33 | BIO *out = NULL; | |
34 | int num = -1; | |
5270e702 | 35 | char *engine=NULL; |
27b78273 BM |
36 | |
37 | apps_startup(); | |
38 | ||
39 | if (bio_err == NULL) | |
40 | if ((bio_err = BIO_new(BIO_s_file())) != NULL) | |
41 | BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); | |
42 | ||
43 | badopt = 0; | |
44 | i = 0; | |
45 | while (!badopt && argv[++i] != NULL) | |
46 | { | |
47 | if (strcmp(argv[i], "-out") == 0) | |
48 | { | |
49 | if ((argv[i+1] != NULL) && (outfile == NULL)) | |
50 | outfile = argv[++i]; | |
51 | else | |
52 | badopt = 1; | |
53 | } | |
73fc98a7 | 54 | else if (strcmp(argv[i], "-engine") == 0) |
5270e702 RL |
55 | { |
56 | if ((argv[i+1] != NULL) && (engine == NULL)) | |
57 | engine = argv[++i]; | |
58 | else | |
59 | badopt = 1; | |
60 | } | |
27b78273 BM |
61 | else if (strcmp(argv[i], "-rand") == 0) |
62 | { | |
63 | if ((argv[i+1] != NULL) && (inrand == NULL)) | |
64 | inrand = argv[++i]; | |
65 | else | |
66 | badopt = 1; | |
67 | } | |
68 | else if (strcmp(argv[i], "-base64") == 0) | |
69 | { | |
70 | if (!base64) | |
71 | base64 = 1; | |
72 | else | |
73 | badopt = 1; | |
74 | } | |
d9586857 | 75 | else if (isdigit((unsigned char)argv[i][0])) |
27b78273 BM |
76 | { |
77 | if (num < 0) | |
78 | { | |
79 | r = sscanf(argv[i], "%d", &num); | |
80 | if (r == 0 || num < 0) | |
81 | badopt = 1; | |
82 | } | |
83 | else | |
84 | badopt = 1; | |
85 | } | |
86 | else | |
87 | badopt = 1; | |
88 | } | |
89 | ||
90 | if (num < 0) | |
91 | badopt = 1; | |
92 | ||
93 | if (badopt) | |
94 | { | |
95 | BIO_printf(bio_err, "Usage: rand [options] num\n"); | |
96 | BIO_printf(bio_err, "where options are\n"); | |
5270e702 | 97 | BIO_printf(bio_err, "-out file - write to file\n"); |
1613c4d3 | 98 | BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n"); |
5270e702 RL |
99 | BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); |
100 | BIO_printf(bio_err, "-base64 - encode output\n"); | |
27b78273 BM |
101 | goto err; |
102 | } | |
103 | ||
5270e702 RL |
104 | if (engine != NULL) |
105 | { | |
106 | if((e = ENGINE_by_id(engine)) == NULL) | |
107 | { | |
108 | BIO_printf(bio_err,"invalid engine \"%s\"\n", | |
109 | engine); | |
110 | goto err; | |
111 | } | |
112 | if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) | |
113 | { | |
114 | BIO_printf(bio_err,"can't use that engine\n"); | |
115 | goto err; | |
116 | } | |
117 | BIO_printf(bio_err,"engine \"%s\" set.\n", engine); | |
118 | /* Free our "structural" reference. */ | |
119 | ENGINE_free(e); | |
120 | } | |
121 | ||
f365611c | 122 | app_RAND_load_file(NULL, bio_err, (inrand != NULL)); |
27b78273 BM |
123 | if (inrand != NULL) |
124 | BIO_printf(bio_err,"%ld semi-random bytes loaded\n", | |
125 | app_RAND_load_files(inrand)); | |
126 | ||
127 | out = BIO_new(BIO_s_file()); | |
128 | if (out == NULL) | |
129 | goto err; | |
130 | if (outfile != NULL) | |
131 | r = BIO_write_filename(out, outfile); | |
132 | else | |
645749ef | 133 | { |
27b78273 | 134 | r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); |
bc36ee62 | 135 | #ifdef OPENSSL_SYS_VMS |
645749ef RL |
136 | { |
137 | BIO *tmpbio = BIO_new(BIO_f_linebuffer()); | |
138 | out = BIO_push(tmpbio, out); | |
139 | } | |
140 | #endif | |
141 | } | |
27b78273 BM |
142 | if (r <= 0) |
143 | goto err; | |
144 | ||
145 | if (base64) | |
146 | { | |
147 | BIO *b64 = BIO_new(BIO_f_base64()); | |
148 | if (b64 == NULL) | |
149 | goto err; | |
150 | out = BIO_push(b64, out); | |
151 | } | |
152 | ||
153 | while (num > 0) | |
154 | { | |
155 | unsigned char buf[4096]; | |
156 | int chunk; | |
157 | ||
158 | chunk = num; | |
159 | if (chunk > sizeof buf) | |
160 | chunk = sizeof buf; | |
161 | r = RAND_bytes(buf, chunk); | |
162 | if (r <= 0) | |
163 | goto err; | |
164 | BIO_write(out, buf, chunk); | |
165 | num -= chunk; | |
166 | } | |
167 | BIO_flush(out); | |
168 | ||
169 | app_RAND_write_file(NULL, bio_err); | |
170 | ret = 0; | |
171 | ||
172 | err: | |
173 | ERR_print_errors(bio_err); | |
174 | if (out) | |
175 | BIO_free_all(out); | |
176 | EXIT(ret); | |
177 | } |