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