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