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