dsa: apps deprecation changes
[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 #include <openssl/encoder.h>
26
27 /*
28  * TODO: This include is to get OSSL_KEYMGMT_SELECT_*, which feels a bit
29  * much just for those macros...  they might serve better as EVP macros.
30  */
31 #include <openssl/core_dispatch.h>
32
33 #ifndef OPENSSL_NO_RC4
34 # define DEFAULT_PVK_ENCR_STRENGTH      2
35 #else
36 # define DEFAULT_PVK_ENCR_STRENGTH      0
37 #endif
38
39 typedef enum OPTION_choice {
40     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
41     OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
42     OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
43     OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
44     /* Do not change the order here; see case statements below */
45     OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
46     OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER,
47     OPT_PROV_ENUM, OPT_TRADITIONAL
48 } OPTION_CHOICE;
49
50 const OPTIONS rsa_options[] = {
51     OPT_SECTION("General"),
52     {"help", OPT_HELP, '-', "Display this summary"},
53     {"check", OPT_CHECK, '-', "Verify key consistency"},
54     {"", OPT_CIPHER, '-', "Any supported cipher"},
55 #ifndef OPENSSL_NO_ENGINE
56     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
57 #endif
58
59     OPT_SECTION("Input"),
60     {"in", OPT_IN, 's', "Input file"},
61     {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE"},
62     {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
63     {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
64     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
65
66     OPT_SECTION("Output"),
67     {"out", OPT_OUT, '>', "Output file"},
68     {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
69     {"pubout", OPT_PUBOUT, '-', "Output a public key"},
70     {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
71     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
72     {"noout", OPT_NOOUT, '-', "Don't print key out"},
73     {"text", OPT_TEXT, '-', "Print the key in text"},
74     {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
75     {"traditional", OPT_TRADITIONAL, '-',
76      "Use traditional format for private keys"},
77
78 #ifndef OPENSSL_NO_RC4
79     OPT_SECTION("PVK"),
80     {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
81     {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
82     {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
83 #endif
84
85     OPT_PROV_OPTIONS,
86     {NULL}
87 };
88
89 int rsa_main(int argc, char **argv)
90 {
91     ENGINE *e = NULL;
92     BIO *out = NULL;
93     EVP_PKEY *pkey = NULL;
94     EVP_PKEY_CTX *pctx;
95     const EVP_CIPHER *enc = NULL;
96     char *infile = NULL, *outfile = NULL, *prog;
97     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
98     int private = 0;
99     int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
100     int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
101     int pvk_encr = DEFAULT_PVK_ENCR_STRENGTH;
102     OPTION_CHOICE o;
103     int traditional = 0;
104     const char *output_type = NULL;
105     const char *output_structure = NULL;
106     int selection = 0;
107     OSSL_ENCODER_CTX *ectx = NULL;
108
109     prog = opt_init(argc, argv, rsa_options);
110     while ((o = opt_next()) != OPT_EOF) {
111         switch (o) {
112         case OPT_EOF:
113         case OPT_ERR:
114  opthelp:
115             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
116             goto end;
117         case OPT_HELP:
118             opt_help(rsa_options);
119             ret = 0;
120             goto end;
121         case OPT_INFORM:
122             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
123                 goto opthelp;
124             break;
125         case OPT_IN:
126             infile = opt_arg();
127             break;
128         case OPT_OUTFORM:
129             if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
130                 goto opthelp;
131             break;
132         case OPT_OUT:
133             outfile = opt_arg();
134             break;
135         case OPT_PASSIN:
136             passinarg = opt_arg();
137             break;
138         case OPT_PASSOUT:
139             passoutarg = opt_arg();
140             break;
141         case OPT_ENGINE:
142             e = setup_engine(opt_arg(), 0);
143             break;
144         case OPT_PUBIN:
145             pubin = 1;
146             break;
147         case OPT_PUBOUT:
148             pubout = 1;
149             break;
150         case OPT_RSAPUBKEY_IN:
151             pubin = 2;
152             break;
153         case OPT_RSAPUBKEY_OUT:
154             pubout = 2;
155             break;
156         case OPT_PVK_STRONG:    /* pvk_encr:= 2 */
157         case OPT_PVK_WEAK:      /* pvk_encr:= 1 */
158         case OPT_PVK_NONE:      /* pvk_encr:= 0 */
159             pvk_encr = (o - OPT_PVK_NONE);
160             break;
161         case OPT_NOOUT:
162             noout = 1;
163             break;
164         case OPT_TEXT:
165             text = 1;
166             break;
167         case OPT_MODULUS:
168             modulus = 1;
169             break;
170         case OPT_CHECK:
171             check = 1;
172             break;
173         case OPT_CIPHER:
174             if (!opt_cipher(opt_unknown(), &enc))
175                 goto opthelp;
176             break;
177         case OPT_PROV_CASES:
178             if (!opt_provider(o))
179                 goto end;
180             break;
181         case OPT_TRADITIONAL:
182             traditional = 1;
183             break;
184         }
185     }
186
187     /* No extra arguments. */
188     argc = opt_num_rest();
189     if (argc != 0)
190         goto opthelp;
191
192     private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
193
194     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
195         BIO_printf(bio_err, "Error getting passwords\n");
196         goto end;
197     }
198     if (check && pubin) {
199         BIO_printf(bio_err, "Only private keys can be checked\n");
200         goto end;
201     }
202
203     if (pubin) {
204         int tmpformat = -1;
205
206         if (pubin == 2) {
207             if (informat == FORMAT_PEM)
208                 tmpformat = FORMAT_PEMRSA;
209             else if (informat == FORMAT_ASN1)
210                 tmpformat = FORMAT_ASN1RSA;
211         } else {
212             tmpformat = informat;
213         }
214
215         pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
216     } else {
217         pkey = load_key(infile, informat, 1, passin, e, "private key");
218     }
219
220     if (pkey == NULL) {
221         ERR_print_errors(bio_err);
222         goto end;
223     }
224     if (!EVP_PKEY_is_a(pkey, "RSA")) {
225         BIO_printf(bio_err, "Not an RSA key\n");
226         goto end;
227     }
228
229     out = bio_open_owner(outfile, outformat, private);
230     if (out == NULL)
231         goto end;
232
233     if (text) {
234         assert(pubin || private);
235         if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
236             || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
237             perror(outfile);
238             ERR_print_errors(bio_err);
239             goto end;
240         }
241     }
242
243     if (modulus) {
244         BIGNUM *n = NULL;
245
246         /* Every RSA key has an 'n' */
247         EVP_PKEY_get_bn_param(pkey, "n", &n);
248         BIO_printf(out, "Modulus=");
249         BN_print(out, n);
250         BIO_printf(out, "\n");
251         BN_free(n);
252     }
253
254     if (check) {
255         int r;
256
257         pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
258         if (pctx == NULL) {
259             BIO_printf(out, "RSA unable to create PKEY context\n");
260             ERR_print_errors(bio_err);
261             goto end;
262         }
263         r = EVP_PKEY_check(pctx);
264         EVP_PKEY_CTX_free(pctx);
265
266         if (r == 1) {
267             BIO_printf(out, "RSA key ok\n");
268         } else if (r == 0) {
269             unsigned long err;
270
271             while ((err = ERR_peek_error()) != 0 &&
272                    ERR_GET_LIB(err) == ERR_LIB_RSA &&
273                    ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
274                 BIO_printf(out, "RSA key error: %s\n",
275                            ERR_reason_error_string(err));
276                 ERR_get_error(); /* remove err from error stack */
277             }
278         } else if (r == -1) {
279             ERR_print_errors(bio_err);
280             goto end;
281         }
282     }
283
284     if (noout) {
285         ret = 0;
286         goto end;
287     }
288     BIO_printf(bio_err, "writing RSA key\n");
289
290     /* Choose output type for the format */
291     if (outformat == FORMAT_ASN1) {
292         output_type = "DER";
293     } else if (outformat == FORMAT_PEM) {
294         output_type = "PEM";
295     } else if (outformat == FORMAT_MSBLOB) {
296         output_type = "MSBLOB";
297     } else if (outformat == FORMAT_PVK) {
298         if (pubin) {
299             BIO_printf(bio_err, "PVK form impossible with public key input\n");
300             goto end;
301         }
302         output_type = "PVK";
303     } else {
304         BIO_printf(bio_err, "bad output format specified for outfile\n");
305         goto end;
306     }
307
308     /* Select what you want in the output */
309     if (pubout || pubin) {
310         selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
311     } else {
312         assert(private);
313         selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
314                      | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
315     }
316
317     /* For DER based output, select the desired output structure */
318     if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
319         if (pubout || pubin) {
320             if (pubout == 2)
321                 output_structure = "pkcs1"; /* "type-specific" would work too */
322             else
323                 output_structure = "SubjectPublicKeyInfo";
324         } else {
325             assert(private);
326             if (traditional)
327                 output_structure = "pkcs1"; /* "type-specific" would work too */
328             else
329                 output_structure = "pkcs8";
330         }
331     }
332
333     /* Now, perform the encoding */
334     ectx = OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, selection,
335                                             output_type, output_structure,
336                                             NULL);
337     if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
338         BIO_printf(bio_err, "%s format not supported\n", output_type);
339         goto end;
340     }
341
342     /* PVK is a bit special... */
343     if (outformat == FORMAT_PVK) {
344         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
345
346         params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
347         if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
348             BIO_printf(bio_err, "invalid PVK encryption level\n");
349             goto end;
350         }
351     }
352
353     if (!OSSL_ENCODER_to_bio(ectx, out)) {
354         BIO_printf(bio_err, "unable to write key\n");
355         ERR_print_errors(bio_err);
356         goto end;
357     }
358     ret = 0;
359  end:
360     OSSL_ENCODER_CTX_free(ectx);
361     release_engine(e);
362     BIO_free_all(out);
363     EVP_PKEY_free(pkey);
364     OPENSSL_free(passin);
365     OPENSSL_free(passout);
366     return ret;
367 }