Move CMS enveloping code out of the algorithms and into CMS
[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, OPT_TRADITIONAL
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     {"traditional", OPT_TRADITIONAL, '-',
63      "Use traditional format for private keys"},
64
65 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
66     OPT_SECTION("PVK"),
67     {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
68     {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
69     {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
70 #endif
71
72     OPT_PROV_OPTIONS,
73     {NULL}
74 };
75
76 int rsa_main(int argc, char **argv)
77 {
78     ENGINE *e = NULL;
79     BIO *out = NULL;
80     RSA *rsa = NULL;
81     EVP_PKEY *pkey = NULL;
82     EVP_PKEY_CTX *pctx;
83     const EVP_CIPHER *enc = NULL;
84     char *infile = NULL, *outfile = NULL, *prog;
85     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
86     int i, private = 0;
87     int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
88     int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
89 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
90     int pvk_encr = 2;
91 #endif
92     OPTION_CHOICE o;
93     int traditional = 0;
94
95     prog = opt_init(argc, argv, rsa_options);
96     while ((o = opt_next()) != OPT_EOF) {
97         switch (o) {
98         case OPT_EOF:
99         case OPT_ERR:
100  opthelp:
101             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
102             goto end;
103         case OPT_HELP:
104             opt_help(rsa_options);
105             ret = 0;
106             goto end;
107         case OPT_INFORM:
108             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
109                 goto opthelp;
110             break;
111         case OPT_IN:
112             infile = opt_arg();
113             break;
114         case OPT_OUTFORM:
115             if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
116                 goto opthelp;
117             break;
118         case OPT_OUT:
119             outfile = opt_arg();
120             break;
121         case OPT_PASSIN:
122             passinarg = opt_arg();
123             break;
124         case OPT_PASSOUT:
125             passoutarg = opt_arg();
126             break;
127         case OPT_ENGINE:
128             e = setup_engine(opt_arg(), 0);
129             break;
130         case OPT_PUBIN:
131             pubin = 1;
132             break;
133         case OPT_PUBOUT:
134             pubout = 1;
135             break;
136         case OPT_RSAPUBKEY_IN:
137             pubin = 2;
138             break;
139         case OPT_RSAPUBKEY_OUT:
140             pubout = 2;
141             break;
142         case OPT_PVK_STRONG:    /* pvk_encr:= 2 */
143         case OPT_PVK_WEAK:      /* pvk_encr:= 1 */
144         case OPT_PVK_NONE:      /* pvk_encr:= 0 */
145 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
146             pvk_encr = (o - OPT_PVK_NONE);
147 #endif
148             break;
149         case OPT_NOOUT:
150             noout = 1;
151             break;
152         case OPT_TEXT:
153             text = 1;
154             break;
155         case OPT_MODULUS:
156             modulus = 1;
157             break;
158         case OPT_CHECK:
159             check = 1;
160             break;
161         case OPT_CIPHER:
162             if (!opt_cipher(opt_unknown(), &enc))
163                 goto opthelp;
164             break;
165         case OPT_PROV_CASES:
166             if (!opt_provider(o))
167                 goto end;
168             break;
169         case OPT_TRADITIONAL:
170             traditional = 1;
171             break;
172         }
173     }
174     argc = opt_num_rest();
175     if (argc != 0)
176         goto opthelp;
177
178     private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
179
180     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
181         BIO_printf(bio_err, "Error getting passwords\n");
182         goto end;
183     }
184     if (check && pubin) {
185         BIO_printf(bio_err, "Only private keys can be checked\n");
186         goto end;
187     }
188
189     if (pubin) {
190         int tmpformat = -1;
191
192         if (pubin == 2) {
193             if (informat == FORMAT_PEM)
194                 tmpformat = FORMAT_PEMRSA;
195             else if (informat == FORMAT_ASN1)
196                 tmpformat = FORMAT_ASN1RSA;
197         } else {
198             tmpformat = informat;
199         }
200
201         pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
202     } else {
203         pkey = load_key(infile, informat, 1, passin, e, "private key");
204     }
205
206     if (pkey != NULL)
207         rsa = EVP_PKEY_get1_RSA(pkey);
208
209     if (rsa == NULL) {
210         ERR_print_errors(bio_err);
211         goto end;
212     }
213
214     out = bio_open_owner(outfile, outformat, private);
215     if (out == NULL)
216         goto end;
217
218     if (text) {
219         assert(pubin || private);
220         if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
221             || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
222             perror(outfile);
223             ERR_print_errors(bio_err);
224             goto end;
225         }
226     }
227
228     if (modulus) {
229         const BIGNUM *n;
230         RSA_get0_key(rsa, &n, NULL, NULL);
231         BIO_printf(out, "Modulus=");
232         BN_print(out, n);
233         BIO_printf(out, "\n");
234     }
235
236     if (check) {
237         int r;
238
239         pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
240         if (pctx == NULL) {
241             BIO_printf(out, "RSA unable to create PKEY context\n");
242             ERR_print_errors(bio_err);
243             goto end;
244         }
245         r = EVP_PKEY_check(pctx);
246         EVP_PKEY_CTX_free(pctx);
247
248         if (r == 1) {
249             BIO_printf(out, "RSA key ok\n");
250         } else if (r == 0) {
251             unsigned long err;
252
253             while ((err = ERR_peek_error()) != 0 &&
254                    ERR_GET_LIB(err) == ERR_LIB_RSA &&
255                    ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
256                 BIO_printf(out, "RSA key error: %s\n",
257                            ERR_reason_error_string(err));
258                 ERR_get_error(); /* remove err from error stack */
259             }
260         } else if (r == -1) {
261             ERR_print_errors(bio_err);
262             goto end;
263         }
264     }
265
266     if (noout) {
267         ret = 0;
268         goto end;
269     }
270     BIO_printf(bio_err, "writing RSA key\n");
271     if (outformat == FORMAT_ASN1) {
272         if (pubout || pubin) {
273             if (pubout == 2)
274                 i = i2d_RSAPublicKey_bio(out, rsa);
275             else
276                 i = i2d_RSA_PUBKEY_bio(out, rsa);
277         } else {
278             assert(private);
279             i = i2d_RSAPrivateKey_bio(out, rsa);
280         }
281     } else if (outformat == FORMAT_PEM) {
282         if (pubout || pubin) {
283             if (pubout == 2)
284                 i = PEM_write_bio_RSAPublicKey(out, rsa);
285             else
286                 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
287         } else {
288             assert(private);
289             if (traditional) {
290                 i = PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
291                                                          NULL, passout);
292             } else {
293                 i = PEM_write_bio_PrivateKey(out, pkey,
294                                              enc, NULL, 0, NULL, passout);
295             }
296         }
297 #ifndef OPENSSL_NO_DSA
298     } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
299         EVP_PKEY *pk;
300         pk = EVP_PKEY_new();
301         if (pk == NULL)
302             goto end;
303
304         EVP_PKEY_set1_RSA(pk, rsa);
305         if (outformat == FORMAT_PVK) {
306             if (pubin) {
307                 BIO_printf(bio_err, "PVK form impossible with public key input\n");
308                 EVP_PKEY_free(pk);
309                 goto end;
310             }
311             assert(private);
312 # ifdef OPENSSL_NO_RC4
313             BIO_printf(bio_err, "PVK format not supported\n");
314             EVP_PKEY_free(pk);
315             goto end;
316 # else
317             i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
318 # endif
319         } else if (pubin || pubout) {
320             i = i2b_PublicKey_bio(out, pk);
321         } else {
322             assert(private);
323             i = i2b_PrivateKey_bio(out, pk);
324         }
325         EVP_PKEY_free(pk);
326 #endif
327     } else {
328         BIO_printf(bio_err, "bad output format specified for outfile\n");
329         goto end;
330     }
331     if (i <= 0) {
332         BIO_printf(bio_err, "unable to write key\n");
333         ERR_print_errors(bio_err);
334     } else {
335         ret = 0;
336     }
337  end:
338     release_engine(e);
339     BIO_free_all(out);
340     EVP_PKEY_free(pkey);
341     RSA_free(rsa);
342     OPENSSL_free(passin);
343     OPENSSL_free(passout);
344     return ret;
345 }