Restore makedepend capabilities for Windows and VMS
[openssl.git] / apps / genrsa.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11 #ifdef OPENSSL_NO_RSA
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
14
15 # include <stdio.h>
16 # include <string.h>
17 # include <sys/types.h>
18 # include <sys/stat.h>
19 # include "apps.h"
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/bn.h>
23 # include <openssl/rsa.h>
24 # include <openssl/evp.h>
25 # include <openssl/x509.h>
26 # include <openssl/pem.h>
27 # include <openssl/rand.h>
28
29 # define DEFBITS 2048
30 # define DEFPRIMES 2
31
32 static int genrsa_cb(int p, int n, BN_GENCB *cb);
33
34 typedef enum OPTION_choice {
35     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
36     OPT_3, OPT_F4, OPT_ENGINE,
37     OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES,
38     OPT_R_ENUM
39 } OPTION_CHOICE;
40
41 const OPTIONS genrsa_options[] = {
42     {"help", OPT_HELP, '-', "Display this summary"},
43     {"3", OPT_3, '-', "Use 3 for the E value"},
44     {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
45     {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
46     {"out", OPT_OUT, 's', "Output the key to specified file"},
47     OPT_R_OPTIONS,
48     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
49     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
50 # ifndef OPENSSL_NO_ENGINE
51     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
52 # endif
53     {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
54     {NULL}
55 };
56
57 int genrsa_main(int argc, char **argv)
58 {
59     BN_GENCB *cb = BN_GENCB_new();
60     PW_CB_DATA cb_data;
61     ENGINE *eng = NULL;
62     BIGNUM *bn = BN_new();
63     BIO *out = NULL;
64     const BIGNUM *e;
65     RSA *rsa = NULL;
66     const EVP_CIPHER *enc = NULL;
67     int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
68     unsigned long f4 = RSA_F4;
69     char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
70     char *prog, *hexe, *dece;
71     OPTION_CHOICE o;
72
73     if (bn == NULL || cb == NULL)
74         goto end;
75
76     BN_GENCB_set(cb, genrsa_cb, bio_err);
77
78     prog = opt_init(argc, argv, genrsa_options);
79     while ((o = opt_next()) != OPT_EOF) {
80         switch (o) {
81         case OPT_EOF:
82         case OPT_ERR:
83 opthelp:
84             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85             goto end;
86         case OPT_HELP:
87             ret = 0;
88             opt_help(genrsa_options);
89             goto end;
90         case OPT_3:
91             f4 = 3;
92             break;
93         case OPT_F4:
94             f4 = RSA_F4;
95             break;
96         case OPT_OUT:
97             outfile = opt_arg();
98             break;
99         case OPT_ENGINE:
100             eng = setup_engine(opt_arg(), 0);
101             break;
102         case OPT_R_CASES:
103             if (!opt_rand(o))
104                 goto end;
105             break;
106         case OPT_PASSOUT:
107             passoutarg = opt_arg();
108             break;
109         case OPT_CIPHER:
110             if (!opt_cipher(opt_unknown(), &enc))
111                 goto end;
112             break;
113         case OPT_PRIMES:
114             if (!opt_int(opt_arg(), &primes))
115                 goto end;
116             break;
117         }
118     }
119     argc = opt_num_rest();
120     argv = opt_rest();
121
122     if (argc == 1) {
123         if (!opt_int(argv[0], &num) || num <= 0)
124             goto end;
125     } else if (argc > 0) {
126         BIO_printf(bio_err, "Extra arguments given.\n");
127         goto opthelp;
128     }
129
130     private = 1;
131     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
132         BIO_printf(bio_err, "Error getting password\n");
133         goto end;
134     }
135
136     out = bio_open_owner(outfile, FORMAT_PEM, private);
137     if (out == NULL)
138         goto end;
139
140     BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
141                num, primes);
142     rsa = eng ? RSA_new_method(eng) : RSA_new();
143     if (rsa == NULL)
144         goto end;
145
146     if (!BN_set_word(bn, f4)
147         || !RSA_generate_multi_prime_key(rsa, num, primes, bn, cb))
148         goto end;
149
150     RSA_get0_key(rsa, NULL, &e, NULL);
151     hexe = BN_bn2hex(e);
152     dece = BN_bn2dec(e);
153     if (hexe && dece) {
154         BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
155     }
156     OPENSSL_free(hexe);
157     OPENSSL_free(dece);
158     cb_data.password = passout;
159     cb_data.prompt_info = outfile;
160     assert(private);
161     if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0,
162                                      (pem_password_cb *)password_callback,
163                                      &cb_data))
164         goto end;
165
166     ret = 0;
167  end:
168     BN_free(bn);
169     BN_GENCB_free(cb);
170     RSA_free(rsa);
171     BIO_free_all(out);
172     release_engine(eng);
173     OPENSSL_free(passout);
174     if (ret != 0)
175         ERR_print_errors(bio_err);
176     return ret;
177 }
178
179 static int genrsa_cb(int p, int n, BN_GENCB *cb)
180 {
181     char c = '*';
182
183     if (p == 0)
184         c = '.';
185     if (p == 1)
186         c = '+';
187     if (p == 2)
188         c = '*';
189     if (p == 3)
190         c = '\n';
191     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
192     (void)BIO_flush(BN_GENCB_get_arg(cb));
193     return 1;
194 }
195 #endif