xref: /openssl/apps/rsautl.c (revision da1c088f)
1 /*
2  * Copyright 2000-2023 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_COMMON,
30     OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
31     OPT_RSA_RAW, OPT_OAEP, 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, by default an RSA private key"},
51     {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"},
52     {"pubin", OPT_PUBIN, '-', "Input key is an RSA public pkey"},
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     {"raw", OPT_RSA_RAW, '-', "Use no padding"},
60     {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
61     {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
62     {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
63     {"asn1parse", OPT_ASN1PARSE, '-',
64      "Run output through asn1parse; useful with -verify"},
65     {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
66 
67     OPT_R_OPTIONS,
68     OPT_PROV_OPTIONS,
69     {NULL}
70 };
71 
rsautl_main(int argc,char ** argv)72 int rsautl_main(int argc, char **argv)
73 {
74     BIO *in = NULL, *out = NULL;
75     ENGINE *e = NULL;
76     EVP_PKEY *pkey = NULL;
77     EVP_PKEY_CTX *ctx = NULL;
78     X509 *x;
79     char *infile = NULL, *outfile = NULL, *keyfile = NULL;
80     char *passinarg = NULL, *passin = NULL, *prog;
81     char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
82     unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING;
83     size_t rsa_inlen, rsa_outlen = 0;
84     int keyformat = FORMAT_UNDEF, keysize, ret = 1, rv;
85     int hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
86     OPTION_CHOICE o;
87 
88     prog = opt_init(argc, argv, rsautl_options);
89     while ((o = opt_next()) != OPT_EOF) {
90         switch (o) {
91         case OPT_EOF:
92         case OPT_ERR:
93  opthelp:
94             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
95             goto end;
96         case OPT_HELP:
97             opt_help(rsautl_options);
98             ret = 0;
99             goto end;
100         case OPT_KEYFORM:
101             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
102                 goto opthelp;
103             break;
104         case OPT_IN:
105             infile = opt_arg();
106             break;
107         case OPT_OUT:
108             outfile = opt_arg();
109             break;
110         case OPT_ENGINE:
111             e = setup_engine(opt_arg(), 0);
112             break;
113         case OPT_ASN1PARSE:
114             asn1parse = 1;
115             break;
116         case OPT_HEXDUMP:
117             hexdump = 1;
118             break;
119         case OPT_RSA_RAW:
120             pad = RSA_NO_PADDING;
121             break;
122         case OPT_OAEP:
123             pad = RSA_PKCS1_OAEP_PADDING;
124             break;
125         case OPT_PKCS:
126             pad = RSA_PKCS1_PADDING;
127             break;
128         case OPT_X931:
129             pad = RSA_X931_PADDING;
130             break;
131         case OPT_SIGN:
132             rsa_mode = RSA_SIGN;
133             need_priv = 1;
134             break;
135         case OPT_VERIFY:
136             rsa_mode = RSA_VERIFY;
137             break;
138         case OPT_REV:
139             rev = 1;
140             break;
141         case OPT_ENCRYPT:
142             rsa_mode = RSA_ENCRYPT;
143             break;
144         case OPT_DECRYPT:
145             rsa_mode = RSA_DECRYPT;
146             need_priv = 1;
147             break;
148         case OPT_PUBIN:
149             key_type = KEY_PUBKEY;
150             break;
151         case OPT_CERTIN:
152             key_type = KEY_CERT;
153             break;
154         case OPT_INKEY:
155             keyfile = opt_arg();
156             break;
157         case OPT_PASSIN:
158             passinarg = opt_arg();
159             break;
160         case OPT_R_CASES:
161             if (!opt_rand(o))
162                 goto end;
163             break;
164         case OPT_PROV_CASES:
165             if (!opt_provider(o))
166                 goto end;
167             break;
168         }
169     }
170 
171     /* No extra arguments. */
172     if (!opt_check_rest_arg(NULL))
173         goto opthelp;
174 
175     if (!app_RAND_load())
176         goto end;
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, FORMAT_UNDEF, "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_get_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) > 0
246             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
247             && EVP_PKEY_verify_recover(ctx, rsa_out, &rsa_outlen,
248                                        rsa_in, rsa_inlen) > 0;
249         break;
250     case RSA_SIGN:
251         rv = EVP_PKEY_sign_init(ctx) > 0
252             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
253             && EVP_PKEY_sign(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen) > 0;
254         break;
255     case RSA_ENCRYPT:
256         rv = EVP_PKEY_encrypt_init(ctx) > 0
257             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
258             && EVP_PKEY_encrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen) > 0;
259         break;
260     case RSA_DECRYPT:
261         rv = EVP_PKEY_decrypt_init(ctx) > 0
262             && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
263             && EVP_PKEY_decrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen) > 0;
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 }
293