diff options
Diffstat (limited to 'src/lib/libssl/src/apps/genrsa.c')
-rw-r--r-- | src/lib/libssl/src/apps/genrsa.c | 104 |
1 files changed, 40 insertions, 64 deletions
diff --git a/src/lib/libssl/src/apps/genrsa.c b/src/lib/libssl/src/apps/genrsa.c index 67382065fb..dc63ff02bd 100644 --- a/src/lib/libssl/src/apps/genrsa.c +++ b/src/lib/libssl/src/apps/genrsa.c | |||
@@ -63,7 +63,6 @@ | |||
63 | #include <sys/stat.h> | 63 | #include <sys/stat.h> |
64 | #include "apps.h" | 64 | #include "apps.h" |
65 | #include <openssl/bio.h> | 65 | #include <openssl/bio.h> |
66 | #include <openssl/rand.h> | ||
67 | #include <openssl/err.h> | 66 | #include <openssl/err.h> |
68 | #include <openssl/bn.h> | 67 | #include <openssl/bn.h> |
69 | #include <openssl/rsa.h> | 68 | #include <openssl/rsa.h> |
@@ -76,18 +75,20 @@ | |||
76 | #define PROG genrsa_main | 75 | #define PROG genrsa_main |
77 | 76 | ||
78 | static void MS_CALLBACK genrsa_cb(int p, int n, void *arg); | 77 | static void MS_CALLBACK genrsa_cb(int p, int n, void *arg); |
79 | static long gr_load_rand(char *names); | 78 | |
79 | int MAIN(int, char **); | ||
80 | |||
80 | int MAIN(int argc, char **argv) | 81 | int MAIN(int argc, char **argv) |
81 | { | 82 | { |
82 | int ret=1; | 83 | int ret=1; |
83 | char buffer[200]; | ||
84 | RSA *rsa=NULL; | 84 | RSA *rsa=NULL; |
85 | int i,num=DEFBITS; | 85 | int i,num=DEFBITS; |
86 | long rnum=0,l; | 86 | long l; |
87 | EVP_CIPHER *enc=NULL; | 87 | EVP_CIPHER *enc=NULL; |
88 | unsigned long f4=RSA_F4; | 88 | unsigned long f4=RSA_F4; |
89 | char *outfile=NULL; | 89 | char *outfile=NULL; |
90 | char *inrand=NULL,*randfile; | 90 | char *passargout = NULL, *passout = NULL; |
91 | char *inrand=NULL; | ||
91 | BIO *out=NULL; | 92 | BIO *out=NULL; |
92 | 93 | ||
93 | apps_startup(); | 94 | apps_startup(); |
@@ -97,7 +98,7 @@ int MAIN(int argc, char **argv) | |||
97 | BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); | 98 | BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); |
98 | if ((out=BIO_new(BIO_s_file())) == NULL) | 99 | if ((out=BIO_new(BIO_s_file())) == NULL) |
99 | { | 100 | { |
100 | BIO_printf(bio_err,"unable to creat BIO for output\n"); | 101 | BIO_printf(bio_err,"unable to create BIO for output\n"); |
101 | goto err; | 102 | goto err; |
102 | } | 103 | } |
103 | 104 | ||
@@ -130,6 +131,11 @@ int MAIN(int argc, char **argv) | |||
130 | else if (strcmp(*argv,"-idea") == 0) | 131 | else if (strcmp(*argv,"-idea") == 0) |
131 | enc=EVP_idea_cbc(); | 132 | enc=EVP_idea_cbc(); |
132 | #endif | 133 | #endif |
134 | else if (strcmp(*argv,"-passout") == 0) | ||
135 | { | ||
136 | if (--argc < 1) goto bad; | ||
137 | passargout= *(++argv); | ||
138 | } | ||
133 | else | 139 | else |
134 | break; | 140 | break; |
135 | argv++; | 141 | argv++; |
@@ -139,21 +145,28 @@ int MAIN(int argc, char **argv) | |||
139 | { | 145 | { |
140 | bad: | 146 | bad: |
141 | BIO_printf(bio_err,"usage: genrsa [args] [numbits]\n"); | 147 | BIO_printf(bio_err,"usage: genrsa [args] [numbits]\n"); |
142 | BIO_printf(bio_err," -des - encrypt the generated key with DES in cbc mode\n"); | 148 | BIO_printf(bio_err," -des encrypt the generated key with DES in cbc mode\n"); |
143 | BIO_printf(bio_err," -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); | 149 | BIO_printf(bio_err," -des3 encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); |
144 | #ifndef NO_IDEA | 150 | #ifndef NO_IDEA |
145 | BIO_printf(bio_err," -idea - encrypt the generated key with IDEA in cbc mode\n"); | 151 | BIO_printf(bio_err," -idea encrypt the generated key with IDEA in cbc mode\n"); |
146 | #endif | 152 | #endif |
147 | BIO_printf(bio_err," -out file - output the key to 'file\n"); | 153 | BIO_printf(bio_err," -out file output the key to 'file\n"); |
148 | BIO_printf(bio_err," -f4 - use F4 (0x10001) for the E value\n"); | 154 | BIO_printf(bio_err," -passout arg output file pass phrase source\n"); |
149 | BIO_printf(bio_err," -3 - use 3 for the E value\n"); | 155 | BIO_printf(bio_err," -f4 use F4 (0x10001) for the E value\n"); |
156 | BIO_printf(bio_err," -3 use 3 for the E value\n"); | ||
150 | BIO_printf(bio_err," -rand file:file:...\n"); | 157 | BIO_printf(bio_err," -rand file:file:...\n"); |
151 | BIO_printf(bio_err," - load the file (or the files in the directory) into\n"); | 158 | BIO_printf(bio_err," load the file (or the files in the directory) into\n"); |
152 | BIO_printf(bio_err," the random number generator\n"); | 159 | BIO_printf(bio_err," the random number generator\n"); |
153 | goto err; | 160 | goto err; |
154 | } | 161 | } |
155 | 162 | ||
156 | ERR_load_crypto_strings(); | 163 | ERR_load_crypto_strings(); |
164 | |||
165 | if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { | ||
166 | BIO_printf(bio_err, "Error getting password\n"); | ||
167 | goto err; | ||
168 | } | ||
169 | |||
157 | if (outfile == NULL) | 170 | if (outfile == NULL) |
158 | BIO_set_fp(out,stdout,BIO_NOCLOSE); | 171 | BIO_set_fp(out,stdout,BIO_NOCLOSE); |
159 | else | 172 | else |
@@ -165,45 +178,23 @@ bad: | |||
165 | } | 178 | } |
166 | } | 179 | } |
167 | 180 | ||
168 | #ifdef WINDOWS | 181 | if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) |
169 | BIO_printf(bio_err,"Loading 'screen' into random state -"); | ||
170 | BIO_flush(bio_err); | ||
171 | RAND_screen(); | ||
172 | BIO_printf(bio_err," done\n"); | ||
173 | #endif | ||
174 | randfile=RAND_file_name(buffer,200); | ||
175 | if ((randfile == NULL) || | ||
176 | !(rnum=(long)RAND_load_file(randfile,1024L*1024L))) | ||
177 | { | 182 | { |
178 | BIO_printf(bio_err,"unable to load 'random state'\n"); | 183 | BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); |
179 | } | 184 | } |
180 | 185 | if (inrand != NULL) | |
181 | if (inrand == NULL) | 186 | BIO_printf(bio_err,"%ld semi-random bytes loaded\n", |
182 | { | 187 | app_RAND_load_files(inrand)); |
183 | if (rnum == 0) | ||
184 | { | ||
185 | BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); | ||
186 | } | ||
187 | } | ||
188 | else | ||
189 | { | ||
190 | rnum+=gr_load_rand(inrand); | ||
191 | } | ||
192 | if (rnum != 0) | ||
193 | BIO_printf(bio_err,"%ld semi-random bytes loaded\n",rnum); | ||
194 | 188 | ||
195 | BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n", | 189 | BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n", |
196 | num); | 190 | num); |
197 | rsa=RSA_generate_key(num,f4,genrsa_cb,bio_err); | 191 | rsa=RSA_generate_key(num,f4,genrsa_cb,bio_err); |
198 | 192 | ||
199 | if (randfile == NULL) | 193 | app_RAND_write_file(NULL, bio_err); |
200 | BIO_printf(bio_err,"unable to write 'random state'\n"); | ||
201 | else | ||
202 | RAND_write_file(randfile); | ||
203 | 194 | ||
204 | if (rsa == NULL) goto err; | 195 | if (rsa == NULL) goto err; |
205 | 196 | ||
206 | /* We need to do the folloing for when the base number size is < | 197 | /* We need to do the following for when the base number size is < |
207 | * long, esp windows 3.1 :-(. */ | 198 | * long, esp windows 3.1 :-(. */ |
208 | l=0L; | 199 | l=0L; |
209 | for (i=0; i<rsa->e->top; i++) | 200 | for (i=0; i<rsa->e->top; i++) |
@@ -215,13 +206,14 @@ bad: | |||
215 | l+=rsa->e->d[i]; | 206 | l+=rsa->e->d[i]; |
216 | } | 207 | } |
217 | BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l); | 208 | BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l); |
218 | if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,NULL,NULL)) | 209 | if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,NULL, passout)) |
219 | goto err; | 210 | goto err; |
220 | 211 | ||
221 | ret=0; | 212 | ret=0; |
222 | err: | 213 | err: |
223 | if (rsa != NULL) RSA_free(rsa); | 214 | if (rsa != NULL) RSA_free(rsa); |
224 | if (out != NULL) BIO_free(out); | 215 | if (out != NULL) BIO_free(out); |
216 | if(passout) Free(passout); | ||
225 | if (ret != 0) | 217 | if (ret != 0) |
226 | ERR_print_errors(bio_err); | 218 | ERR_print_errors(bio_err); |
227 | EXIT(ret); | 219 | EXIT(ret); |
@@ -241,26 +233,10 @@ static void MS_CALLBACK genrsa_cb(int p, int n, void *arg) | |||
241 | p=n; | 233 | p=n; |
242 | #endif | 234 | #endif |
243 | } | 235 | } |
236 | #else /* !NO_RSA */ | ||
244 | 237 | ||
245 | static long gr_load_rand(char *name) | 238 | # if PEDANTIC |
246 | { | 239 | static void *dummy=&dummy; |
247 | char *p,*n; | 240 | # endif |
248 | int last; | ||
249 | long tot=0; | ||
250 | |||
251 | for (;;) | ||
252 | { | ||
253 | last=0; | ||
254 | for (p=name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++); | ||
255 | if (*p == '\0') last=1; | ||
256 | *p='\0'; | ||
257 | n=name; | ||
258 | name=p+1; | ||
259 | if (*n == '\0') break; | ||
260 | 241 | ||
261 | tot+=RAND_load_file(n,1024L*1024L); | ||
262 | if (last) break; | ||
263 | } | ||
264 | return(tot); | ||
265 | } | ||
266 | #endif | 242 | #endif |