Deprecate RSA harder
[openssl.git] / apps / rsautl.c
1 /*
2  * Copyright 2000-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 "apps.h"
13 #include "progs.h"
14 #include <string.h>
15 #include <openssl/err.h>
16 #include <openssl/pem.h>
17 #include <openssl/rsa.h>
18
19 #define RSA_SIGN        1
20 #define RSA_VERIFY      2
21 #define RSA_ENCRYPT     3
22 #define RSA_DECRYPT     4
23
24 #define KEY_PRIVKEY     1
25 #define KEY_PUBKEY      2
26 #define KEY_CERT        3
27
28 typedef enum OPTION_choice {
29     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30     OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
31     OPT_RSA_RAW, OPT_OAEP, OPT_SSL, OPT_PKCS, OPT_X931,
32     OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
33     OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM,
34     OPT_R_ENUM, OPT_PROV_ENUM
35 } OPTION_CHOICE;
36
37 const OPTIONS rsautl_options[] = {
38     OPT_SECTION("General"),
39     {"help", OPT_HELP, '-', "Display this summary"},
40     {"sign", OPT_SIGN, '-', "Sign with private key"},
41     {"verify", OPT_VERIFY, '-', "Verify with public key"},
42     {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"},
43     {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"},
44 #ifndef OPENSSL_NO_ENGINE
45     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
46 #endif
47
48     OPT_SECTION("Input"),
49     {"in", OPT_IN, '<', "Input file"},
50     {"inkey", OPT_INKEY, 's', "Input key"},
51     {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"},
52     {"pubin", OPT_PUBIN, '-', "Input is an RSA public"},
53     {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"},
54     {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
55     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
56
57     OPT_SECTION("Output"),
58     {"out", OPT_OUT, '>', "Output file"},
59     {"ssl", OPT_SSL, '-', "Use SSL v2 padding"},
60     {"raw", OPT_RSA_RAW, '-', "Use no padding"},
61     {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
62     {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
63     {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
64     {"asn1parse", OPT_ASN1PARSE, '-',
65      "Run output through asn1parse; useful with -verify"},
66     {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
67
68     OPT_R_OPTIONS,
69     OPT_PROV_OPTIONS,
70     {NULL}
71 };
72
73 int rsautl_main(int argc, char **argv)
74 {
75     BIO *in = NULL, *out = NULL;
76     ENGINE *e = NULL;
77     EVP_PKEY *pkey = NULL;
78     EVP_PKEY_CTX *ctx = NULL;
79     X509 *x;
80     char *infile = NULL, *outfile = NULL, *keyfile = NULL;
81     char *passinarg = NULL, *passin = NULL, *prog;
82     char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
83     unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING;
84     size_t rsa_inlen, rsa_outlen = 0;
85     int keyformat = FORMAT_PEM, keysize, ret = 1, rv;
86     int hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
87     OPTION_CHOICE o;
88
89     prog = opt_init(argc, argv, rsautl_options);
90     while ((o = opt_next()) != OPT_EOF) {
91         switch (o) {
92         case OPT_EOF:
93         case OPT_ERR:
94  opthelp:
95             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
96             goto end;
97         case OPT_HELP:
98             opt_help(rsautl_options);
99             ret = 0;
100             goto end;
101         case OPT_KEYFORM:
102             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
103                 goto opthelp;
104             break;
105         case OPT_IN:
106             infile = opt_arg();
107             break;
108         case OPT_OUT:
109             outfile = opt_arg();
110             break;
111         case OPT_ENGINE:
112             e = setup_engine(opt_arg(), 0);
113             break;
114         case OPT_ASN1PARSE:
115             asn1parse = 1;
116             break;
117         case OPT_HEXDUMP:
118             hexdump = 1;
119             break;
120         case OPT_RSA_RAW:
121             pad = RSA_NO_PADDING;
122             break;
123         case OPT_OAEP:
124             pad = RSA_PKCS1_OAEP_PADDING;
125             break;
126         case OPT_SSL:
127             pad = RSA_SSLV23_PADDING;
128             break;
129         case OPT_PKCS:
130             pad = RSA_PKCS1_PADDING;
131             break;
132         case OPT_X931:
133             pad = RSA_X931_PADDING;
134             break;
135         case OPT_SIGN:
136             rsa_mode = RSA_SIGN;
137             need_priv = 1;
138             break;
139         case OPT_VERIFY:
140             rsa_mode = RSA_VERIFY;
141             break;
142         case OPT_REV:
143             rev = 1;
144             break;
145         case OPT_ENCRYPT:
146             rsa_mode = RSA_ENCRYPT;
147             break;
148         case OPT_DECRYPT:
149             rsa_mode = RSA_DECRYPT;
150             need_priv = 1;
151             break;
152         case OPT_PUBIN:
153             key_type = KEY_PUBKEY;
154             break;
155         case OPT_CERTIN:
156             key_type = KEY_CERT;
157             break;
158         case OPT_INKEY:
159             keyfile = opt_arg();
160             break;
161         case OPT_PASSIN:
162             passinarg = opt_arg();
163             break;
164         case OPT_R_CASES:
165             if (!opt_rand(o))
166                 goto end;
167             break;
168         case OPT_PROV_CASES:
169             if (!opt_provider(o))
170                 goto end;
171             break;
172         }
173     }
174     argc = opt_num_rest();
175     if (argc != 0)
176         goto opthelp;
177
178     if (need_priv && (key_type != KEY_PRIVKEY)) {
179         BIO_printf(bio_err, "A private key is needed for this operation\n");
180         goto end;
181     }
182
183     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
184         BIO_printf(bio_err, "Error getting password\n");
185         goto end;
186     }
187
188     switch (key_type) {
189     case KEY_PRIVKEY:
190         pkey = load_key(keyfile, keyformat, 0, passin, e, "private key");
191         break;
192
193     case KEY_PUBKEY:
194         pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "public key");
195         break;
196
197     case KEY_CERT:
198         x = load_cert(keyfile, "Certificate");
199         if (x) {
200             pkey = X509_get_pubkey(x);
201             X509_free(x);
202         }
203         break;
204     }
205
206     if (pkey == NULL)
207         return 1;
208
209     in = bio_open_default(infile, 'r', FORMAT_BINARY);
210     if (in == NULL)
211         goto end;
212     out = bio_open_default(outfile, 'w', FORMAT_BINARY);
213     if (out == NULL)
214         goto end;
215
216     keysize = EVP_PKEY_size(pkey);
217
218     rsa_in = app_malloc(keysize * 2, "hold rsa key");
219     rsa_out = app_malloc(keysize, "output rsa key");
220     rsa_outlen = keysize;
221
222     /* Read the input data */
223     rv = BIO_read(in, rsa_in, keysize * 2);
224     if (rv < 0) {
225         BIO_printf(bio_err, "Error reading input Data\n");
226         goto end;
227     }
228     rsa_inlen = rv;
229     if (rev) {
230         size_t i;
231         unsigned char ctmp;
232
233         for (i = 0; i < rsa_inlen / 2; i++) {
234             ctmp = rsa_in[i];
235             rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
236             rsa_in[rsa_inlen - 1 - i] = ctmp;
237         }
238     }
239
240     if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL)
241         goto end;
242
243     switch (rsa_mode) {
244     case RSA_VERIFY:
245         rv = EVP_PKEY_verify_recover_init(ctx)
246             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
247             && EVP_PKEY_verify_recover(ctx, rsa_out, &rsa_outlen,
248                                        rsa_in, rsa_inlen);
249         break;
250     case RSA_SIGN:
251         rv = EVP_PKEY_sign_init(ctx)
252             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
253             && EVP_PKEY_sign(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen);
254         break;
255     case RSA_ENCRYPT:
256         rv = EVP_PKEY_encrypt_init(ctx)
257             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
258             && EVP_PKEY_encrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen);
259         break;
260     case RSA_DECRYPT:
261         rv = EVP_PKEY_decrypt_init(ctx)
262             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
263             && EVP_PKEY_decrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen);
264         break;
265     }
266
267     if (!rv) {
268         BIO_printf(bio_err, "RSA operation error\n");
269         ERR_print_errors(bio_err);
270         goto end;
271     }
272     ret = 0;
273     if (asn1parse) {
274         if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
275             ERR_print_errors(bio_err);
276         }
277     } else if (hexdump) {
278         BIO_dump(out, (char *)rsa_out, rsa_outlen);
279     } else {
280         BIO_write(out, rsa_out, rsa_outlen);
281     }
282  end:
283     EVP_PKEY_CTX_free(ctx);
284     EVP_PKEY_free(pkey);
285     release_engine(e);
286     BIO_free(in);
287     BIO_free_all(out);
288     OPENSSL_free(rsa_in);
289     OPENSSL_free(rsa_out);
290     OPENSSL_free(passin);
291     return ret;
292 }