1e71b7053SJung-uk Kim /*
2*ad991e4cSEd Maste * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
874664626SKris Kennaway */
974664626SKris Kennaway
103b4e3dcbSSimon L. B. Nielsen #include <openssl/opensslconf.h>
11b077aed3SPierre Pronchery
1274664626SKris Kennaway #include <stdio.h>
1374664626SKris Kennaway #include <string.h>
1474664626SKris Kennaway #include <sys/types.h>
1574664626SKris Kennaway #include <sys/stat.h>
1674664626SKris Kennaway #include "apps.h"
17e71b7053SJung-uk Kim #include "progs.h"
1874664626SKris Kennaway #include <openssl/bio.h>
1974664626SKris Kennaway #include <openssl/err.h>
2074664626SKris Kennaway #include <openssl/bn.h>
2174664626SKris Kennaway #include <openssl/rsa.h>
2274664626SKris Kennaway #include <openssl/evp.h>
2374664626SKris Kennaway #include <openssl/x509.h>
2474664626SKris Kennaway #include <openssl/pem.h>
255c87c606SMark Murray #include <openssl/rand.h>
2674664626SKris Kennaway
277bded2dbSJung-uk Kim #define DEFBITS 2048
28e71b7053SJung-uk Kim #define DEFPRIMES 2
2974664626SKris Kennaway
30b077aed3SPierre Pronchery static int verbose = 0;
31b077aed3SPierre Pronchery
32b077aed3SPierre Pronchery static int genrsa_cb(EVP_PKEY_CTX *ctx);
33f579bf8eSKris Kennaway
34e71b7053SJung-uk Kim typedef enum OPTION_choice {
35b077aed3SPierre Pronchery OPT_COMMON,
36b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DEPRECATED_3_0
37b077aed3SPierre Pronchery OPT_3,
38b077aed3SPierre Pronchery #endif
39b077aed3SPierre Pronchery OPT_F4, OPT_ENGINE,
40b077aed3SPierre Pronchery OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE,
41b077aed3SPierre Pronchery OPT_R_ENUM, OPT_PROV_ENUM, OPT_TRADITIONAL
42e71b7053SJung-uk Kim } OPTION_CHOICE;
43f579bf8eSKris Kennaway
44e71b7053SJung-uk Kim const OPTIONS genrsa_options[] = {
45b077aed3SPierre Pronchery {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"},
46b077aed3SPierre Pronchery
47b077aed3SPierre Pronchery OPT_SECTION("General"),
48e71b7053SJung-uk Kim {"help", OPT_HELP, '-', "Display this summary"},
49fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
50e71b7053SJung-uk Kim {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
51fceca8a3SJacques Vidrine #endif
52b077aed3SPierre Pronchery
53b077aed3SPierre Pronchery OPT_SECTION("Input"),
54b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DEPRECATED_3_0
55b077aed3SPierre Pronchery {"3", OPT_3, '-', "(deprecated) Use 3 for the E value"},
56b077aed3SPierre Pronchery #endif
57b077aed3SPierre Pronchery {"F4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
58b077aed3SPierre Pronchery {"f4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
59b077aed3SPierre Pronchery
60b077aed3SPierre Pronchery OPT_SECTION("Output"),
61b077aed3SPierre Pronchery {"out", OPT_OUT, '>', "Output the key to specified file"},
62b077aed3SPierre Pronchery {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
63e71b7053SJung-uk Kim {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
64b077aed3SPierre Pronchery {"verbose", OPT_VERBOSE, '-', "Verbose output"},
65b077aed3SPierre Pronchery {"traditional", OPT_TRADITIONAL, '-',
66b077aed3SPierre Pronchery "Use traditional format for private keys"},
67b077aed3SPierre Pronchery {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
68b077aed3SPierre Pronchery
69b077aed3SPierre Pronchery OPT_R_OPTIONS,
70b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
71b077aed3SPierre Pronchery
72b077aed3SPierre Pronchery OPT_PARAMETERS(),
73b077aed3SPierre Pronchery {"numbits", 0, 0, "Size of key in bits"},
74e71b7053SJung-uk Kim {NULL}
75e71b7053SJung-uk Kim };
7674664626SKris Kennaway
genrsa_main(int argc,char ** argv)77e71b7053SJung-uk Kim int genrsa_main(int argc, char **argv)
78ddd58736SKris Kennaway {
79e71b7053SJung-uk Kim BN_GENCB *cb = BN_GENCB_new();
80e71b7053SJung-uk Kim ENGINE *eng = NULL;
81e71b7053SJung-uk Kim BIGNUM *bn = BN_new();
82e71b7053SJung-uk Kim BIO *out = NULL;
83b077aed3SPierre Pronchery EVP_PKEY *pkey = NULL;
84b077aed3SPierre Pronchery EVP_PKEY_CTX *ctx = NULL;
85b077aed3SPierre Pronchery EVP_CIPHER *enc = NULL;
86e71b7053SJung-uk Kim int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
87e71b7053SJung-uk Kim unsigned long f4 = RSA_F4;
88e71b7053SJung-uk Kim char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
89b077aed3SPierre Pronchery char *prog, *hexe, *dece, *ciphername = NULL;
90e71b7053SJung-uk Kim OPTION_CHOICE o;
91b077aed3SPierre Pronchery int traditional = 0;
92e71b7053SJung-uk Kim
93e71b7053SJung-uk Kim if (bn == NULL || cb == NULL)
94e71b7053SJung-uk Kim goto end;
95e71b7053SJung-uk Kim
96e71b7053SJung-uk Kim prog = opt_init(argc, argv, genrsa_options);
97e71b7053SJung-uk Kim while ((o = opt_next()) != OPT_EOF) {
98e71b7053SJung-uk Kim switch (o) {
99e71b7053SJung-uk Kim case OPT_EOF:
100e71b7053SJung-uk Kim case OPT_ERR:
101e71b7053SJung-uk Kim opthelp:
102e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
103e71b7053SJung-uk Kim goto end;
104e71b7053SJung-uk Kim case OPT_HELP:
105e71b7053SJung-uk Kim ret = 0;
106e71b7053SJung-uk Kim opt_help(genrsa_options);
107e71b7053SJung-uk Kim goto end;
108b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DEPRECATED_3_0
109e71b7053SJung-uk Kim case OPT_3:
110b077aed3SPierre Pronchery f4 = RSA_3;
111e71b7053SJung-uk Kim break;
112b077aed3SPierre Pronchery #endif
113e71b7053SJung-uk Kim case OPT_F4:
114e71b7053SJung-uk Kim f4 = RSA_F4;
115e71b7053SJung-uk Kim break;
116e71b7053SJung-uk Kim case OPT_OUT:
117e71b7053SJung-uk Kim outfile = opt_arg();
118e71b7053SJung-uk Kim break;
119e71b7053SJung-uk Kim case OPT_ENGINE:
120e71b7053SJung-uk Kim eng = setup_engine(opt_arg(), 0);
121e71b7053SJung-uk Kim break;
122e71b7053SJung-uk Kim case OPT_R_CASES:
123e71b7053SJung-uk Kim if (!opt_rand(o))
124e71b7053SJung-uk Kim goto end;
125e71b7053SJung-uk Kim break;
126b077aed3SPierre Pronchery case OPT_PROV_CASES:
127b077aed3SPierre Pronchery if (!opt_provider(o))
128b077aed3SPierre Pronchery goto end;
129b077aed3SPierre Pronchery break;
130e71b7053SJung-uk Kim case OPT_PASSOUT:
131e71b7053SJung-uk Kim passoutarg = opt_arg();
132e71b7053SJung-uk Kim break;
133e71b7053SJung-uk Kim case OPT_CIPHER:
134b077aed3SPierre Pronchery ciphername = opt_unknown();
135e71b7053SJung-uk Kim break;
136e71b7053SJung-uk Kim case OPT_PRIMES:
137b077aed3SPierre Pronchery primes = opt_int_arg();
138b077aed3SPierre Pronchery break;
139b077aed3SPierre Pronchery case OPT_VERBOSE:
140b077aed3SPierre Pronchery verbose = 1;
141b077aed3SPierre Pronchery break;
142b077aed3SPierre Pronchery case OPT_TRADITIONAL:
143b077aed3SPierre Pronchery traditional = 1;
144e71b7053SJung-uk Kim break;
145e71b7053SJung-uk Kim }
146e71b7053SJung-uk Kim }
147b077aed3SPierre Pronchery
148b077aed3SPierre Pronchery /* One optional argument, the bitsize. */
149e71b7053SJung-uk Kim argc = opt_num_rest();
150e71b7053SJung-uk Kim argv = opt_rest();
151e71b7053SJung-uk Kim
152e71b7053SJung-uk Kim if (argc == 1) {
153e71b7053SJung-uk Kim if (!opt_int(argv[0], &num) || num <= 0)
154e71b7053SJung-uk Kim goto end;
155e71b7053SJung-uk Kim if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
156e71b7053SJung-uk Kim BIO_printf(bio_err,
157e71b7053SJung-uk Kim "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
158e71b7053SJung-uk Kim " Your key size is %d! Larger key size may behave not as expected.\n",
159e71b7053SJung-uk Kim OPENSSL_RSA_MAX_MODULUS_BITS, num);
160e71b7053SJung-uk Kim } else if (argc > 0) {
161e71b7053SJung-uk Kim BIO_printf(bio_err, "Extra arguments given.\n");
162e71b7053SJung-uk Kim goto opthelp;
163e71b7053SJung-uk Kim }
164e71b7053SJung-uk Kim
165b077aed3SPierre Pronchery if (!app_RAND_load())
166b077aed3SPierre Pronchery goto end;
167b077aed3SPierre Pronchery
168e71b7053SJung-uk Kim private = 1;
169b077aed3SPierre Pronchery if (ciphername != NULL) {
170b077aed3SPierre Pronchery if (!opt_cipher(ciphername, &enc))
171b077aed3SPierre Pronchery goto end;
172b077aed3SPierre Pronchery }
173e71b7053SJung-uk Kim if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
174e71b7053SJung-uk Kim BIO_printf(bio_err, "Error getting password\n");
175e71b7053SJung-uk Kim goto end;
176e71b7053SJung-uk Kim }
177e71b7053SJung-uk Kim
178e71b7053SJung-uk Kim out = bio_open_owner(outfile, FORMAT_PEM, private);
179e71b7053SJung-uk Kim if (out == NULL)
180e71b7053SJung-uk Kim goto end;
181e71b7053SJung-uk Kim
182b077aed3SPierre Pronchery if (!init_gen_str(&ctx, "RSA", eng, 0, app_get0_libctx(),
183b077aed3SPierre Pronchery app_get0_propq()))
184e71b7053SJung-uk Kim goto end;
185e71b7053SJung-uk Kim
186b077aed3SPierre Pronchery EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
187b077aed3SPierre Pronchery EVP_PKEY_CTX_set_app_data(ctx, bio_err);
188e71b7053SJung-uk Kim
189b077aed3SPierre Pronchery if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
190b077aed3SPierre Pronchery BIO_printf(bio_err, "Error setting RSA length\n");
191b077aed3SPierre Pronchery goto end;
192b077aed3SPierre Pronchery }
193b077aed3SPierre Pronchery if (!BN_set_word(bn, f4)) {
194b077aed3SPierre Pronchery BIO_printf(bio_err, "Error allocating RSA public exponent\n");
195b077aed3SPierre Pronchery goto end;
196b077aed3SPierre Pronchery }
197b077aed3SPierre Pronchery if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
198b077aed3SPierre Pronchery BIO_printf(bio_err, "Error setting RSA public exponent\n");
199b077aed3SPierre Pronchery goto end;
200b077aed3SPierre Pronchery }
201b077aed3SPierre Pronchery if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
202b077aed3SPierre Pronchery BIO_printf(bio_err, "Error setting number of primes\n");
203b077aed3SPierre Pronchery goto end;
204b077aed3SPierre Pronchery }
205b077aed3SPierre Pronchery pkey = app_keygen(ctx, "RSA", num, verbose);
206*ad991e4cSEd Maste if (pkey == NULL)
207*ad991e4cSEd Maste goto end;
208b077aed3SPierre Pronchery
209b077aed3SPierre Pronchery if (verbose) {
210b077aed3SPierre Pronchery BIGNUM *e = NULL;
211b077aed3SPierre Pronchery
212b077aed3SPierre Pronchery /* Every RSA key has an 'e' */
213b077aed3SPierre Pronchery EVP_PKEY_get_bn_param(pkey, "e", &e);
214b077aed3SPierre Pronchery if (e == NULL) {
215b077aed3SPierre Pronchery BIO_printf(bio_err, "Error cannot access RSA e\n");
216b077aed3SPierre Pronchery goto end;
217b077aed3SPierre Pronchery }
218e71b7053SJung-uk Kim hexe = BN_bn2hex(e);
219e71b7053SJung-uk Kim dece = BN_bn2dec(e);
220e71b7053SJung-uk Kim if (hexe && dece) {
221e71b7053SJung-uk Kim BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
222e71b7053SJung-uk Kim }
223e71b7053SJung-uk Kim OPENSSL_free(hexe);
224e71b7053SJung-uk Kim OPENSSL_free(dece);
225b077aed3SPierre Pronchery BN_free(e);
226b077aed3SPierre Pronchery }
227b077aed3SPierre Pronchery if (traditional) {
228b077aed3SPierre Pronchery if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
229b077aed3SPierre Pronchery NULL, passout))
230e71b7053SJung-uk Kim goto end;
231b077aed3SPierre Pronchery } else {
232b077aed3SPierre Pronchery if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
233b077aed3SPierre Pronchery goto end;
234b077aed3SPierre Pronchery }
23574664626SKris Kennaway
23674664626SKris Kennaway ret = 0;
237e71b7053SJung-uk Kim end:
2386f9291ceSJung-uk Kim BN_free(bn);
239e71b7053SJung-uk Kim BN_GENCB_free(cb);
240b077aed3SPierre Pronchery EVP_PKEY_CTX_free(ctx);
241b077aed3SPierre Pronchery EVP_PKEY_free(pkey);
242b077aed3SPierre Pronchery EVP_CIPHER_free(enc);
2436f9291ceSJung-uk Kim BIO_free_all(out);
244e71b7053SJung-uk Kim release_engine(eng);
2456f9291ceSJung-uk Kim OPENSSL_free(passout);
24674664626SKris Kennaway if (ret != 0)
24774664626SKris Kennaway ERR_print_errors(bio_err);
248e71b7053SJung-uk Kim return ret;
24974664626SKris Kennaway }
25074664626SKris Kennaway
genrsa_cb(EVP_PKEY_CTX * ctx)251b077aed3SPierre Pronchery static int genrsa_cb(EVP_PKEY_CTX *ctx)
25274664626SKris Kennaway {
25374664626SKris Kennaway char c = '*';
254b077aed3SPierre Pronchery BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
255b077aed3SPierre Pronchery int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
256b077aed3SPierre Pronchery
257b077aed3SPierre Pronchery if (!verbose)
258b077aed3SPierre Pronchery return 1;
25974664626SKris Kennaway
2606f9291ceSJung-uk Kim if (p == 0)
2616f9291ceSJung-uk Kim c = '.';
2626f9291ceSJung-uk Kim if (p == 1)
2636f9291ceSJung-uk Kim c = '+';
2646f9291ceSJung-uk Kim if (p == 2)
2656f9291ceSJung-uk Kim c = '*';
2666f9291ceSJung-uk Kim if (p == 3)
2676f9291ceSJung-uk Kim c = '\n';
268b077aed3SPierre Pronchery BIO_write(b, &c, 1);
269b077aed3SPierre Pronchery (void)BIO_flush(b);
2703b4e3dcbSSimon L. B. Nielsen return 1;
27174664626SKris Kennaway }
272