]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 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 |
12 | NON_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" | |
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> | |
d02b48c6 | 28 | |
0f113f3e | 29 | # define DEFBITS 2048 |
d02b48c6 | 30 | |
6d23cf97 | 31 | static int genrsa_cb(int p, int n, BN_GENCB *cb); |
667ac4ec | 32 | |
7e1b7485 RS |
33 | typedef enum OPTION_choice { |
34 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
700b4a4a | 35 | OPT_3, OPT_F4, OPT_ENGINE, |
3ee1eac2 RS |
36 | OPT_OUT, OPT_PASSOUT, OPT_CIPHER, |
37 | OPT_R_ENUM | |
7e1b7485 | 38 | } OPTION_CHOICE; |
667ac4ec | 39 | |
44c83ebd | 40 | const OPTIONS genrsa_options[] = { |
7e1b7485 RS |
41 | {"help", OPT_HELP, '-', "Display this summary"}, |
42 | {"3", OPT_3, '-', "Use 3 for the E value"}, | |
43 | {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"}, | |
44 | {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"}, | |
7e1b7485 | 45 | {"out", OPT_OUT, 's', "Output the key to specified file"}, |
3ee1eac2 | 46 | OPT_R_OPTIONS, |
7e1b7485 RS |
47 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
48 | {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"}, | |
0f113f3e | 49 | # ifndef OPENSSL_NO_ENGINE |
7e1b7485 | 50 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
0f113f3e | 51 | # endif |
7e1b7485 RS |
52 | {NULL} |
53 | }; | |
54 | ||
55 | int genrsa_main(int argc, char **argv) | |
56 | { | |
57 | BN_GENCB *cb = BN_GENCB_new(); | |
3b061a00 | 58 | PW_CB_DATA cb_data; |
9862e9aa | 59 | ENGINE *eng = NULL; |
7e1b7485 RS |
60 | BIGNUM *bn = BN_new(); |
61 | BIO *out = NULL; | |
2ac6115d | 62 | const BIGNUM *e; |
7e1b7485 | 63 | RSA *rsa = NULL; |
0f113f3e | 64 | const EVP_CIPHER *enc = NULL; |
700b4a4a | 65 | int ret = 1, num = DEFBITS, private = 0; |
0f113f3e | 66 | unsigned long f4 = RSA_F4; |
7e1b7485 | 67 | char *outfile = NULL, *passoutarg = NULL, *passout = NULL; |
3ee1eac2 | 68 | char *prog, *hexe, *dece; |
7e1b7485 | 69 | OPTION_CHOICE o; |
348d0d14 | 70 | |
96487cdd | 71 | if (bn == NULL || cb == NULL) |
7e1b7485 | 72 | goto end; |
348d0d14 | 73 | |
0f113f3e | 74 | BN_GENCB_set(cb, genrsa_cb, bio_err); |
d02b48c6 | 75 | |
7e1b7485 RS |
76 | prog = opt_init(argc, argv, genrsa_options); |
77 | while ((o = opt_next()) != OPT_EOF) { | |
78 | switch (o) { | |
79 | case OPT_EOF: | |
80 | case OPT_ERR: | |
81 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
82 | goto end; | |
83 | case OPT_HELP: | |
84 | ret = 0; | |
85 | opt_help(genrsa_options); | |
86 | goto end; | |
87 | case OPT_3: | |
0f113f3e | 88 | f4 = 3; |
7e1b7485 RS |
89 | break; |
90 | case OPT_F4: | |
0f113f3e | 91 | f4 = RSA_F4; |
7e1b7485 | 92 | break; |
7e1b7485 RS |
93 | case OPT_OUT: |
94 | outfile = opt_arg(); | |
902c6b95 | 95 | break; |
7e1b7485 | 96 | case OPT_ENGINE: |
9862e9aa | 97 | eng = setup_engine(opt_arg(), 0); |
7e1b7485 | 98 | break; |
3ee1eac2 RS |
99 | case OPT_R_CASES: |
100 | if (!opt_rand(o)) | |
101 | goto end; | |
7e1b7485 RS |
102 | break; |
103 | case OPT_PASSOUT: | |
104 | passoutarg = opt_arg(); | |
105 | break; | |
106 | case OPT_CIPHER: | |
107 | if (!opt_cipher(opt_unknown(), &enc)) | |
108 | goto end; | |
109 | break; | |
110 | } | |
0f113f3e | 111 | } |
7e1b7485 RS |
112 | argc = opt_num_rest(); |
113 | argv = opt_rest(); | |
3b061a00 | 114 | private = 1; |
a3fe382e | 115 | |
7e1b7485 RS |
116 | if (argv[0] && (!opt_int(argv[0], &num) || num <= 0)) |
117 | goto end; | |
a3fe382e | 118 | |
7e1b7485 | 119 | if (!app_passwd(NULL, passoutarg, NULL, &passout)) { |
0f113f3e | 120 | BIO_printf(bio_err, "Error getting password\n"); |
7e1b7485 | 121 | goto end; |
0f113f3e | 122 | } |
5270e702 | 123 | |
bdd58d98 | 124 | out = bio_open_owner(outfile, FORMAT_PEM, private); |
7e1b7485 RS |
125 | if (out == NULL) |
126 | goto end; | |
d02b48c6 | 127 | |
0f113f3e MC |
128 | BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus\n", |
129 | num); | |
9862e9aa | 130 | rsa = eng ? RSA_new_method(eng) : RSA_new(); |
96487cdd | 131 | if (rsa == NULL) |
7e1b7485 | 132 | goto end; |
0f113f3e | 133 | |
0f113f3e | 134 | if (!BN_set_word(bn, f4) || !RSA_generate_key_ex(rsa, num, bn, cb)) |
7e1b7485 | 135 | goto end; |
dc03504d | 136 | |
9862e9aa RL |
137 | RSA_get0_key(rsa, NULL, &e, NULL); |
138 | hexe = BN_bn2hex(e); | |
139 | dece = BN_bn2dec(e); | |
0f113f3e MC |
140 | if (hexe && dece) { |
141 | BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe); | |
142 | } | |
25aaa98a RS |
143 | OPENSSL_free(hexe); |
144 | OPENSSL_free(dece); | |
3b061a00 RS |
145 | cb_data.password = passout; |
146 | cb_data.prompt_info = outfile; | |
147 | assert(private); | |
148 | if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0, | |
149 | (pem_password_cb *)password_callback, | |
150 | &cb_data)) | |
151 | goto end; | |
d02b48c6 | 152 | |
0f113f3e | 153 | ret = 0; |
7e1b7485 | 154 | end: |
23a1d5e9 RS |
155 | BN_free(bn); |
156 | BN_GENCB_free(cb); | |
d6407083 | 157 | RSA_free(rsa); |
ca3a82c3 | 158 | BIO_free_all(out); |
dd1abd44 | 159 | release_engine(eng); |
b548a1f1 | 160 | OPENSSL_free(passout); |
0f113f3e MC |
161 | if (ret != 0) |
162 | ERR_print_errors(bio_err); | |
7e1b7485 | 163 | return (ret); |
0f113f3e | 164 | } |
d02b48c6 | 165 | |
6d23cf97 | 166 | static int genrsa_cb(int p, int n, BN_GENCB *cb) |
0f113f3e MC |
167 | { |
168 | char c = '*'; | |
d02b48c6 | 169 | |
0f113f3e MC |
170 | if (p == 0) |
171 | c = '.'; | |
172 | if (p == 1) | |
173 | c = '+'; | |
174 | if (p == 2) | |
175 | c = '*'; | |
176 | if (p == 3) | |
177 | c = '\n'; | |
178 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); | |
179 | (void)BIO_flush(BN_GENCB_get_arg(cb)); | |
180 | return 1; | |
181 | } | |
f5d7a031 | 182 | #endif |