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