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