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