summaryrefslogtreecommitdiff
path: root/src/lib/libssl/src/apps/genrsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/src/apps/genrsa.c')
-rw-r--r--src/lib/libssl/src/apps/genrsa.c198
1 files changed, 102 insertions, 96 deletions
diff --git a/src/lib/libssl/src/apps/genrsa.c b/src/lib/libssl/src/apps/genrsa.c
index cdba6189ad..515bd7c901 100644
--- a/src/lib/libssl/src/apps/genrsa.c
+++ b/src/lib/libssl/src/apps/genrsa.c
@@ -56,45 +56,42 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#ifndef OPENSSL_NO_RSA
59#include <stdio.h> 60#include <stdio.h>
60#include <string.h> 61#include <string.h>
61#include <sys/types.h> 62#include <sys/types.h>
62#include <sys/stat.h> 63#include <sys/stat.h>
63#include "apps.h" 64#include "apps.h"
64#include "bio.h" 65#include <openssl/bio.h>
65#include "rand.h" 66#include <openssl/err.h>
66#include "err.h" 67#include <openssl/bn.h>
67#include "bn.h" 68#include <openssl/rsa.h>
68#include "rsa.h" 69#include <openssl/evp.h>
69#include "evp.h" 70#include <openssl/x509.h>
70#include "x509.h" 71#include <openssl/pem.h>
71#include "pem.h" 72#include <openssl/rand.h>
72 73
73#define DEFBITS 512 74#define DEFBITS 512
74#undef PROG 75#undef PROG
75#define PROG genrsa_main 76#define PROG genrsa_main
76 77
77#ifndef NOPROTO 78static void MS_CALLBACK genrsa_cb(int p, int n, void *arg);
78static void MS_CALLBACK genrsa_cb(int p, int n, char *arg); 79
79static long gr_load_rand(char *names); 80int MAIN(int, char **);
80#else
81static void MS_CALLBACK genrsa_cb();
82static long gr_load_rand();
83#endif
84 81
85int MAIN(argc, argv) 82int MAIN(int argc, char **argv)
86int argc;
87char **argv;
88 { 83 {
84 ENGINE *e = NULL;
89 int ret=1; 85 int ret=1;
90 char buffer[200];
91 RSA *rsa=NULL; 86 RSA *rsa=NULL;
92 int i,num=DEFBITS; 87 int i,num=DEFBITS;
93 long rnum=0,l; 88 long l;
94 EVP_CIPHER *enc=NULL; 89 const EVP_CIPHER *enc=NULL;
95 unsigned long f4=RSA_F4; 90 unsigned long f4=RSA_F4;
96 char *outfile=NULL; 91 char *outfile=NULL;
97 char *inrand=NULL,*randfile; 92 char *passargout = NULL, *passout = NULL;
93 char *engine=NULL;
94 char *inrand=NULL;
98 BIO *out=NULL; 95 BIO *out=NULL;
99 96
100 apps_startup(); 97 apps_startup();
@@ -102,9 +99,12 @@ char **argv;
102 if (bio_err == NULL) 99 if (bio_err == NULL)
103 if ((bio_err=BIO_new(BIO_s_file())) != NULL) 100 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
104 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 101 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
102
103 if (!load_config(bio_err, NULL))
104 goto err;
105 if ((out=BIO_new(BIO_s_file())) == NULL) 105 if ((out=BIO_new(BIO_s_file())) == NULL)
106 { 106 {
107 BIO_printf(bio_err,"unable to creat BIO for output\n"); 107 BIO_printf(bio_err,"unable to create BIO for output\n");
108 goto err; 108 goto err;
109 } 109 }
110 110
@@ -120,23 +120,41 @@ char **argv;
120 } 120 }
121 else if (strcmp(*argv,"-3") == 0) 121 else if (strcmp(*argv,"-3") == 0)
122 f4=3; 122 f4=3;
123 else if (strcmp(*argv,"-F4") == 0) 123 else if (strcmp(*argv,"-F4") == 0 || strcmp(*argv,"-f4") == 0)
124 f4=RSA_F4; 124 f4=RSA_F4;
125 else if (strcmp(*argv,"-engine") == 0)
126 {
127 if (--argc < 1) goto bad;
128 engine= *(++argv);
129 }
125 else if (strcmp(*argv,"-rand") == 0) 130 else if (strcmp(*argv,"-rand") == 0)
126 { 131 {
127 if (--argc < 1) goto bad; 132 if (--argc < 1) goto bad;
128 inrand= *(++argv); 133 inrand= *(++argv);
129 } 134 }
130#ifndef NO_DES 135#ifndef OPENSSL_NO_DES
131 else if (strcmp(*argv,"-des") == 0) 136 else if (strcmp(*argv,"-des") == 0)
132 enc=EVP_des_cbc(); 137 enc=EVP_des_cbc();
133 else if (strcmp(*argv,"-des3") == 0) 138 else if (strcmp(*argv,"-des3") == 0)
134 enc=EVP_des_ede3_cbc(); 139 enc=EVP_des_ede3_cbc();
135#endif 140#endif
136#ifndef NO_IDEA 141#ifndef OPENSSL_NO_IDEA
137 else if (strcmp(*argv,"-idea") == 0) 142 else if (strcmp(*argv,"-idea") == 0)
138 enc=EVP_idea_cbc(); 143 enc=EVP_idea_cbc();
139#endif 144#endif
145#ifndef OPENSSL_NO_AES
146 else if (strcmp(*argv,"-aes128") == 0)
147 enc=EVP_aes_128_cbc();
148 else if (strcmp(*argv,"-aes192") == 0)
149 enc=EVP_aes_192_cbc();
150 else if (strcmp(*argv,"-aes256") == 0)
151 enc=EVP_aes_256_cbc();
152#endif
153 else if (strcmp(*argv,"-passout") == 0)
154 {
155 if (--argc < 1) goto bad;
156 passargout= *(++argv);
157 }
140 else 158 else
141 break; 159 break;
142 argv++; 160 argv++;
@@ -146,23 +164,45 @@ char **argv;
146 { 164 {
147bad: 165bad:
148 BIO_printf(bio_err,"usage: genrsa [args] [numbits]\n"); 166 BIO_printf(bio_err,"usage: genrsa [args] [numbits]\n");
149 BIO_printf(bio_err," -des - encrypt the generated key with DES in cbc mode\n"); 167 BIO_printf(bio_err," -des encrypt the generated key with DES in cbc mode\n");
150 BIO_printf(bio_err," -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); 168 BIO_printf(bio_err," -des3 encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
151#ifndef NO_IDEA 169#ifndef OPENSSL_NO_IDEA
152 BIO_printf(bio_err," -idea - encrypt the generated key with IDEA in cbc mode\n"); 170 BIO_printf(bio_err," -idea encrypt the generated key with IDEA in cbc mode\n");
153#endif 171#endif
154 BIO_printf(bio_err," -out file - output the key to 'file\n"); 172#ifndef OPENSSL_NO_AES
155 BIO_printf(bio_err," -f4 - use F4 (0x10001) for the E value\n"); 173 BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
156 BIO_printf(bio_err," -3 - use 3 for the E value\n"); 174 BIO_printf(bio_err," encrypt PEM output with cbc aes\n");
157 BIO_printf(bio_err," -rand file:file:...\n"); 175#endif
158 BIO_printf(bio_err," - load the file (or the files in the directory) into\n"); 176 BIO_printf(bio_err," -out file output the key to 'file\n");
159 BIO_printf(bio_err," the random number generator\n"); 177 BIO_printf(bio_err," -passout arg output file pass phrase source\n");
178 BIO_printf(bio_err," -f4 use F4 (0x10001) for the E value\n");
179 BIO_printf(bio_err," -3 use 3 for the E value\n");
180 BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
181 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
182 BIO_printf(bio_err," load the file (or the files in the directory) into\n");
183 BIO_printf(bio_err," the random number generator\n");
160 goto err; 184 goto err;
161 } 185 }
162 186
163 ERR_load_crypto_strings(); 187 ERR_load_crypto_strings();
188
189 if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
190 BIO_printf(bio_err, "Error getting password\n");
191 goto err;
192 }
193
194 e = setup_engine(bio_err, engine, 0);
195
164 if (outfile == NULL) 196 if (outfile == NULL)
197 {
165 BIO_set_fp(out,stdout,BIO_NOCLOSE); 198 BIO_set_fp(out,stdout,BIO_NOCLOSE);
199#ifdef OPENSSL_SYS_VMS
200 {
201 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
202 out = BIO_push(tmpbio, out);
203 }
204#endif
205 }
166 else 206 else
167 { 207 {
168 if (BIO_write_filename(out,outfile) <= 0) 208 if (BIO_write_filename(out,outfile) <= 0)
@@ -172,45 +212,24 @@ bad:
172 } 212 }
173 } 213 }
174 214
175#ifdef WINDOWS 215 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
176 BIO_printf(bio_err,"Loading 'screen' into random state -"); 216 && !RAND_status())
177 BIO_flush(bio_err);
178 RAND_screen();
179 BIO_printf(bio_err," done\n");
180#endif
181 randfile=RAND_file_name(buffer,200);
182 if ((randfile == NULL) ||
183 !(rnum=(long)RAND_load_file(randfile,1024L*1024L)))
184 { 217 {
185 BIO_printf(bio_err,"unable to load 'random state'\n"); 218 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
186 } 219 }
187 220 if (inrand != NULL)
188 if (inrand == NULL) 221 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
189 { 222 app_RAND_load_files(inrand));
190 if (rnum == 0)
191 {
192 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
193 }
194 }
195 else
196 {
197 rnum+=gr_load_rand(inrand);
198 }
199 if (rnum != 0)
200 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",rnum);
201 223
202 BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n", 224 BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n",
203 num); 225 num);
204 rsa=RSA_generate_key(num,f4,genrsa_cb,(char *)bio_err); 226 rsa=RSA_generate_key(num,f4,genrsa_cb,bio_err);
205 227
206 if (randfile == NULL) 228 app_RAND_write_file(NULL, bio_err);
207 BIO_printf(bio_err,"unable to write 'random state'\n");
208 else
209 RAND_write_file(randfile);
210 229
211 if (rsa == NULL) goto err; 230 if (rsa == NULL) goto err;
212 231
213 /* We need to do the folloing for when the base number size is < 232 /* We need to do the following for when the base number size is <
214 * long, esp windows 3.1 :-(. */ 233 * long, esp windows 3.1 :-(. */
215 l=0L; 234 l=0L;
216 for (i=0; i<rsa->e->top; i++) 235 for (i=0; i<rsa->e->top; i++)
@@ -222,22 +241,27 @@ bad:
222 l+=rsa->e->d[i]; 241 l+=rsa->e->d[i];
223 } 242 }
224 BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l); 243 BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l);
225 if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,NULL)) 244 {
245 PW_CB_DATA cb_data;
246 cb_data.password = passout;
247 cb_data.prompt_info = outfile;
248 if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,
249 (pem_password_cb *)password_callback,&cb_data))
226 goto err; 250 goto err;
251 }
227 252
228 ret=0; 253 ret=0;
229err: 254err:
230 if (rsa != NULL) RSA_free(rsa); 255 if (rsa != NULL) RSA_free(rsa);
231 if (out != NULL) BIO_free(out); 256 if (out != NULL) BIO_free_all(out);
257 if(passout) OPENSSL_free(passout);
232 if (ret != 0) 258 if (ret != 0)
233 ERR_print_errors(bio_err); 259 ERR_print_errors(bio_err);
260 apps_shutdown();
234 EXIT(ret); 261 EXIT(ret);
235 } 262 }
236 263
237static void MS_CALLBACK genrsa_cb(p, n, arg) 264static void MS_CALLBACK genrsa_cb(int p, int n, void *arg)
238int p;
239int n;
240char *arg;
241 { 265 {
242 char c='*'; 266 char c='*';
243 267
@@ -246,33 +270,15 @@ char *arg;
246 if (p == 2) c='*'; 270 if (p == 2) c='*';
247 if (p == 3) c='\n'; 271 if (p == 3) c='\n';
248 BIO_write((BIO *)arg,&c,1); 272 BIO_write((BIO *)arg,&c,1);
249 BIO_flush((BIO *)arg); 273 (void)BIO_flush((BIO *)arg);
250#ifdef LINT 274#ifdef LINT
251 p=n; 275 p=n;
252#endif 276#endif
253 } 277 }
278#else /* !OPENSSL_NO_RSA */
254 279
255static long gr_load_rand(name) 280# if PEDANTIC
256char *name; 281static void *dummy=&dummy;
257 { 282# endif
258 char *p,*n;
259 int last;
260 long tot=0;
261
262 for (;;)
263 {
264 last=0;
265 for (p=name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++);
266 if (*p == '\0') last=1;
267 *p='\0';
268 n=name;
269 name=p+1;
270 if (*n == '\0') break;
271
272 tot+=RAND_load_file(n,1024L*1024L);
273 if (last) break;
274 }
275 return(tot);
276 }
277
278 283
284#endif