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