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