9f91b72d2061547c9ed681c9f3e2e641dffcdd5f
[openssl.git] / apps / rsa.c
1 /*
2  * Copyright 1995-2020 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 <stdlib.h>
14 #include <string.h>
15 #include <time.h>
16 #include "apps.h"
17 #include "progs.h"
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/rsa.h>
21 #include <openssl/evp.h>
22 #include <openssl/x509.h>
23 #include <openssl/pem.h>
24 #include <openssl/bn.h>
25
26 typedef enum OPTION_choice {
27     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
28     OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
29     OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
30     OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
31     /* Do not change the order here; see case statements below */
32     OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
33     OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER,
34     OPT_PROV_ENUM
35 } OPTION_CHOICE;
36
37 const OPTIONS rsa_options[] = {
38     OPT_SECTION("General"),
39     {"help", OPT_HELP, '-', "Display this summary"},
40     {"check", OPT_CHECK, '-', "Verify key consistency"},
41     {"", OPT_CIPHER, '-', "Any supported cipher"},
42 #ifndef OPENSSL_NO_ENGINE
43     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
44 #endif
45
46     OPT_SECTION("Input"),
47     {"in", OPT_IN, 's', "Input file"},
48     {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE"},
49     {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
50     {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
51     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
52
53     OPT_SECTION("Output"),
54     {"out", OPT_OUT, '>', "Output file"},
55     {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
56     {"pubout", OPT_PUBOUT, '-', "Output a public key"},
57     {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
58     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
59     {"noout", OPT_NOOUT, '-', "Don't print key out"},
60     {"text", OPT_TEXT, '-', "Print the key in text"},
61     {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
62
63 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
64     OPT_SECTION("PVK"),
65     {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
66     {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
67     {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
68
69     OPT_PROV_OPTIONS,
70 #endif
71     {NULL}
72 };
73
74 int rsa_main(int argc, char **argv)
75 {
76     ENGINE *e = NULL;
77     BIO *out = NULL;
78     RSA *rsa = NULL;
79     EVP_PKEY *pkey = NULL;
80     EVP_PKEY_CTX *pctx;
81     const EVP_CIPHER *enc = NULL;
82     char *infile = NULL, *outfile = NULL, *prog;
83     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
84     int i, private = 0;
85     int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
86     int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
87 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
88     int pvk_encr = 2;
89 #endif
90     OPTION_CHOICE o;
91
92     prog = opt_init(argc, argv, rsa_options);
93     while ((o = opt_next()) != OPT_EOF) {
94         switch (o) {
95         case OPT_EOF:
96         case OPT_ERR:
97  opthelp:
98             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
99             goto end;
100         case OPT_HELP:
101             opt_help(rsa_options);
102             ret = 0;
103             goto end;
104         case OPT_INFORM:
105             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
106                 goto opthelp;
107             break;
108         case OPT_IN:
109             infile = opt_arg();
110             break;
111         case OPT_OUTFORM:
112             if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
113                 goto opthelp;
114             break;
115         case OPT_OUT:
116             outfile = opt_arg();
117             break;
118         case OPT_PASSIN:
119             passinarg = opt_arg();
120             break;
121         case OPT_PASSOUT:
122             passoutarg = opt_arg();
123             break;
124         case OPT_ENGINE:
125             e = setup_engine(opt_arg(), 0);
126             break;
127         case OPT_PUBIN:
128             pubin = 1;
129             break;
130         case OPT_PUBOUT:
131             pubout = 1;
132             break;
133         case OPT_RSAPUBKEY_IN:
134             pubin = 2;
135             break;
136         case OPT_RSAPUBKEY_OUT:
137             pubout = 2;
138             break;
139         case OPT_PVK_STRONG:    /* pvk_encr:= 2 */
140         case OPT_PVK_WEAK:      /* pvk_encr:= 1 */
141         case OPT_PVK_NONE:      /* pvk_encr:= 0 */
142 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
143             pvk_encr = (o - OPT_PVK_NONE);
144 #endif
145             break;
146         case OPT_NOOUT:
147             noout = 1;
148             break;
149         case OPT_TEXT:
150             text = 1;
151             break;
152         case OPT_MODULUS:
153             modulus = 1;
154             break;
155         case OPT_CHECK:
156             check = 1;
157             break;
158         case OPT_CIPHER:
159             if (!opt_cipher(opt_unknown(), &enc))
160                 goto opthelp;
161             break;
162         case OPT_PROV_CASES:
163             if (!opt_provider(o))
164                 goto end;
165             break;
166         }
167     }
168     argc = opt_num_rest();
169     if (argc != 0)
170         goto opthelp;
171
172     private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
173
174     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
175         BIO_printf(bio_err, "Error getting passwords\n");
176         goto end;
177     }
178     if (check && pubin) {
179         BIO_printf(bio_err, "Only private keys can be checked\n");
180         goto end;
181     }
182
183     if (pubin) {
184         int tmpformat = -1;
185
186         if (pubin == 2) {
187             if (informat == FORMAT_PEM)
188                 tmpformat = FORMAT_PEMRSA;
189             else if (informat == FORMAT_ASN1)
190                 tmpformat = FORMAT_ASN1RSA;
191         } else {
192             tmpformat = informat;
193         }
194
195         pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
196     } else {
197         pkey = load_key(infile, informat, 1, passin, e, "Private Key");
198     }
199
200     if (pkey != NULL)
201         rsa = EVP_PKEY_get1_RSA(pkey);
202
203     if (rsa == NULL) {
204         ERR_print_errors(bio_err);
205         goto end;
206     }
207
208     out = bio_open_owner(outfile, outformat, private);
209     if (out == NULL)
210         goto end;
211
212     if (text) {
213         assert(pubin || private);
214         if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
215             || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
216             perror(outfile);
217             ERR_print_errors(bio_err);
218             goto end;
219         }
220     }
221
222     if (modulus) {
223         const BIGNUM *n;
224         RSA_get0_key(rsa, &n, NULL, NULL);
225         BIO_printf(out, "Modulus=");
226         BN_print(out, n);
227         BIO_printf(out, "\n");
228     }
229
230     if (check) {
231         int r;
232
233         pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
234         if (pctx == NULL) {
235             BIO_printf(out, "RSA unable to create PKEY context\n");
236             ERR_print_errors(bio_err);
237             goto end;
238         }
239         r = EVP_PKEY_check(pctx);
240         EVP_PKEY_CTX_free(pctx);
241
242         if (r == 1) {
243             BIO_printf(out, "RSA key ok\n");
244         } else if (r == 0) {
245             unsigned long err;
246
247             while ((err = ERR_peek_error()) != 0 &&
248                    ERR_GET_LIB(err) == ERR_LIB_RSA &&
249                    ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
250                 BIO_printf(out, "RSA key error: %s\n",
251                            ERR_reason_error_string(err));
252                 ERR_get_error(); /* remove err from error stack */
253             }
254         } else if (r == -1) {
255             ERR_print_errors(bio_err);
256             goto end;
257         }
258     }
259
260     if (noout) {
261         ret = 0;
262         goto end;
263     }
264     BIO_printf(bio_err, "writing RSA key\n");
265     if (outformat == FORMAT_ASN1) {
266         if (pubout || pubin) {
267             if (pubout == 2)
268                 i = i2d_RSAPublicKey_bio(out, rsa);
269             else
270                 i = i2d_RSA_PUBKEY_bio(out, rsa);
271         } else {
272             assert(private);
273             i = i2d_RSAPrivateKey_bio(out, rsa);
274         }
275     } else if (outformat == FORMAT_PEM) {
276         if (pubout || pubin) {
277             if (pubout == 2)
278                 i = PEM_write_bio_RSAPublicKey(out, rsa);
279             else
280                 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
281         } else {
282             assert(private);
283             i = PEM_write_bio_RSAPrivateKey(out, rsa,
284                                             enc, NULL, 0, NULL, passout);
285         }
286 #ifndef OPENSSL_NO_DSA
287     } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
288         EVP_PKEY *pk;
289         pk = EVP_PKEY_new();
290         if (pk == NULL)
291             goto end;
292
293         EVP_PKEY_set1_RSA(pk, rsa);
294         if (outformat == FORMAT_PVK) {
295             if (pubin) {
296                 BIO_printf(bio_err, "PVK form impossible with public key input\n");
297                 EVP_PKEY_free(pk);
298                 goto end;
299             }
300             assert(private);
301 # ifdef OPENSSL_NO_RC4
302             BIO_printf(bio_err, "PVK format not supported\n");
303             EVP_PKEY_free(pk);
304             goto end;
305 # else
306             i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
307 # endif
308         } else if (pubin || pubout) {
309             i = i2b_PublicKey_bio(out, pk);
310         } else {
311             assert(private);
312             i = i2b_PrivateKey_bio(out, pk);
313         }
314         EVP_PKEY_free(pk);
315 #endif
316     } else {
317         BIO_printf(bio_err, "bad output format specified for outfile\n");
318         goto end;
319     }
320     if (i <= 0) {
321         BIO_printf(bio_err, "unable to write key\n");
322         ERR_print_errors(bio_err);
323     } else {
324         ret = 0;
325     }
326  end:
327     release_engine(e);
328     BIO_free_all(out);
329     EVP_PKEY_free(pkey);
330     RSA_free(rsa);
331     OPENSSL_free(passin);
332     OPENSSL_free(passout);
333     return ret;
334 }