xref: /openbsd/usr.bin/openssl/rsautl.c (revision 440d1414)
1*440d1414Stb /* $OpenBSD: rsautl.c,v 1.24 2023/07/23 11:39:29 tb Exp $ */
2dab3f910Sjsing /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3dab3f910Sjsing  * project 2000.
4dab3f910Sjsing  */
5dab3f910Sjsing /* ====================================================================
6dab3f910Sjsing  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7dab3f910Sjsing  *
8dab3f910Sjsing  * Redistribution and use in source and binary forms, with or without
9dab3f910Sjsing  * modification, are permitted provided that the following conditions
10dab3f910Sjsing  * are met:
11dab3f910Sjsing  *
12dab3f910Sjsing  * 1. Redistributions of source code must retain the above copyright
13dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer.
14dab3f910Sjsing  *
15dab3f910Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
16dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer in
17dab3f910Sjsing  *    the documentation and/or other materials provided with the
18dab3f910Sjsing  *    distribution.
19dab3f910Sjsing  *
20dab3f910Sjsing  * 3. All advertising materials mentioning features or use of this
21dab3f910Sjsing  *    software must display the following acknowledgment:
22dab3f910Sjsing  *    "This product includes software developed by the OpenSSL Project
23dab3f910Sjsing  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24dab3f910Sjsing  *
25dab3f910Sjsing  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26dab3f910Sjsing  *    endorse or promote products derived from this software without
27dab3f910Sjsing  *    prior written permission. For written permission, please contact
28dab3f910Sjsing  *    licensing@OpenSSL.org.
29dab3f910Sjsing  *
30dab3f910Sjsing  * 5. Products derived from this software may not be called "OpenSSL"
31dab3f910Sjsing  *    nor may "OpenSSL" appear in their names without prior written
32dab3f910Sjsing  *    permission of the OpenSSL Project.
33dab3f910Sjsing  *
34dab3f910Sjsing  * 6. Redistributions of any form whatsoever must retain the following
35dab3f910Sjsing  *    acknowledgment:
36dab3f910Sjsing  *    "This product includes software developed by the OpenSSL Project
37dab3f910Sjsing  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38dab3f910Sjsing  *
39dab3f910Sjsing  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40dab3f910Sjsing  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41dab3f910Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42dab3f910Sjsing  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43dab3f910Sjsing  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44dab3f910Sjsing  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45dab3f910Sjsing  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46dab3f910Sjsing  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47dab3f910Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48dab3f910Sjsing  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49dab3f910Sjsing  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50dab3f910Sjsing  * OF THE POSSIBILITY OF SUCH DAMAGE.
51dab3f910Sjsing  * ====================================================================
52dab3f910Sjsing  *
53dab3f910Sjsing  * This product includes cryptographic software written by Eric Young
54dab3f910Sjsing  * (eay@cryptsoft.com).  This product includes software written by Tim
55dab3f910Sjsing  * Hudson (tjh@cryptsoft.com).
56dab3f910Sjsing  *
57dab3f910Sjsing  */
58dab3f910Sjsing 
59dab3f910Sjsing #include <openssl/opensslconf.h>
60dab3f910Sjsing 
61dab3f910Sjsing #include <string.h>
62dab3f910Sjsing 
63dab3f910Sjsing #include "apps.h"
64dab3f910Sjsing 
65dab3f910Sjsing #include <openssl/err.h>
66dab3f910Sjsing #include <openssl/pem.h>
67dab3f910Sjsing #include <openssl/rsa.h>
68dab3f910Sjsing 
69dab3f910Sjsing #define RSA_SIGN	1
70dab3f910Sjsing #define RSA_VERIFY	2
71dab3f910Sjsing #define RSA_ENCRYPT	3
72dab3f910Sjsing #define RSA_DECRYPT	4
73dab3f910Sjsing 
74dab3f910Sjsing #define KEY_PRIVKEY	1
75dab3f910Sjsing #define KEY_PUBKEY	2
76dab3f910Sjsing #define KEY_CERT	3
77dab3f910Sjsing 
78efc7e65fStb static struct {
7994e38b1eSjsing 	int asn1parse;
8094e38b1eSjsing 	int hexdump;
8194e38b1eSjsing 	char *infile;
8294e38b1eSjsing 	char *keyfile;
8394e38b1eSjsing 	int keyform;
8494e38b1eSjsing 	int key_type;
8594e38b1eSjsing 	char *outfile;
8694e38b1eSjsing 	int pad;
8794e38b1eSjsing 	char *passargin;
8894e38b1eSjsing 	int rev;
8994e38b1eSjsing 	int rsa_mode;
90e7718adaStb } cfg;
9194e38b1eSjsing 
92ea149709Sguenther static const struct option rsautl_options[] = {
9394e38b1eSjsing 	{
9494e38b1eSjsing 		.name = "asn1parse",
9594e38b1eSjsing 		.desc = "ASN.1 parse the output data",
9694e38b1eSjsing 		.type = OPTION_FLAG,
97e7718adaStb 		.opt.flag = &cfg.asn1parse,
9894e38b1eSjsing 	},
9994e38b1eSjsing 	{
10094e38b1eSjsing 		.name = "certin",
10194e38b1eSjsing 		.desc = "Input is a certificate containing an RSA public key",
10294e38b1eSjsing 		.type = OPTION_VALUE,
10394e38b1eSjsing 		.value = KEY_CERT,
104e7718adaStb 		.opt.value = &cfg.key_type,
10594e38b1eSjsing 	},
10694e38b1eSjsing 	{
10794e38b1eSjsing 		.name = "decrypt",
10894e38b1eSjsing 		.desc = "Decrypt the input data using RSA private key",
10994e38b1eSjsing 		.type = OPTION_VALUE,
11094e38b1eSjsing 		.value = RSA_DECRYPT,
111e7718adaStb 		.opt.value = &cfg.rsa_mode,
11294e38b1eSjsing 	},
11394e38b1eSjsing 	{
11494e38b1eSjsing 		.name = "encrypt",
11594e38b1eSjsing 		.desc = "Encrypt the input data using RSA public key",
11694e38b1eSjsing 		.type = OPTION_VALUE,
11794e38b1eSjsing 		.value = RSA_ENCRYPT,
118e7718adaStb 		.opt.value = &cfg.rsa_mode,
11994e38b1eSjsing 	},
12094e38b1eSjsing 	{
12194e38b1eSjsing 		.name = "hexdump",
12294e38b1eSjsing 		.desc = "Hex dump the output data",
12394e38b1eSjsing 		.type = OPTION_FLAG,
124e7718adaStb 		.opt.flag = &cfg.hexdump,
12594e38b1eSjsing 	},
12694e38b1eSjsing 	{
12794e38b1eSjsing 		.name = "in",
12894e38b1eSjsing 		.argname = "file",
12994e38b1eSjsing 		.desc = "Input file (default stdin)",
13094e38b1eSjsing 		.type = OPTION_ARG,
131e7718adaStb 		.opt.arg = &cfg.infile,
13294e38b1eSjsing 	},
13394e38b1eSjsing 	{
13494e38b1eSjsing 		.name = "inkey",
13594e38b1eSjsing 		.argname = "file",
13694e38b1eSjsing 		.desc = "Input key file",
13794e38b1eSjsing 		.type = OPTION_ARG,
138e7718adaStb 		.opt.arg = &cfg.keyfile,
13994e38b1eSjsing 	},
14094e38b1eSjsing 	{
14194e38b1eSjsing 		.name = "keyform",
14294e38b1eSjsing 		.argname = "fmt",
14394e38b1eSjsing 		.desc = "Input key format (DER, TXT or PEM (default))",
14494e38b1eSjsing 		.type = OPTION_ARG_FORMAT,
145e7718adaStb 		.opt.value = &cfg.keyform,
14694e38b1eSjsing 	},
14794e38b1eSjsing 	{
14894e38b1eSjsing 		.name = "oaep",
14994e38b1eSjsing 		.desc = "Use PKCS#1 OAEP padding",
15094e38b1eSjsing 		.type = OPTION_VALUE,
15194e38b1eSjsing 		.value = RSA_PKCS1_OAEP_PADDING,
152e7718adaStb 		.opt.value = &cfg.pad,
15394e38b1eSjsing 	},
15494e38b1eSjsing 	{
15594e38b1eSjsing 		.name = "out",
15694e38b1eSjsing 		.argname = "file",
15794e38b1eSjsing 		.desc = "Output file (default stdout)",
15894e38b1eSjsing 		.type = OPTION_ARG,
159e7718adaStb 		.opt.arg = &cfg.outfile,
16094e38b1eSjsing 	},
16194e38b1eSjsing 	{
16294e38b1eSjsing 		.name = "passin",
16394e38b1eSjsing 		.argname = "arg",
16494e38b1eSjsing 		.desc = "Key password source",
16594e38b1eSjsing 		.type = OPTION_ARG,
166e7718adaStb 		.opt.arg = &cfg.passargin,
16794e38b1eSjsing 	},
16894e38b1eSjsing 	{
16994e38b1eSjsing 		.name = "pkcs",
17094e38b1eSjsing 		.desc = "Use PKCS#1 v1.5 padding (default)",
17194e38b1eSjsing 		.type = OPTION_VALUE,
17294e38b1eSjsing 		.value = RSA_PKCS1_PADDING,
173e7718adaStb 		.opt.value = &cfg.pad,
17494e38b1eSjsing 	},
17594e38b1eSjsing 	{
17694e38b1eSjsing 		.name = "pubin",
17794e38b1eSjsing 		.desc = "Input is an RSA public key",
17894e38b1eSjsing 		.type = OPTION_VALUE,
17994e38b1eSjsing 		.value = KEY_PUBKEY,
180e7718adaStb 		.opt.value = &cfg.key_type,
18194e38b1eSjsing 	},
18294e38b1eSjsing 	{
18394e38b1eSjsing 		.name = "raw",
18494e38b1eSjsing 		.desc = "Use no padding",
18594e38b1eSjsing 		.type = OPTION_VALUE,
18694e38b1eSjsing 		.value = RSA_NO_PADDING,
187e7718adaStb 		.opt.value = &cfg.pad,
18894e38b1eSjsing 	},
18994e38b1eSjsing 	{
19094e38b1eSjsing 		.name = "rev",
19194e38b1eSjsing 		.desc = "Reverse the input data",
19294e38b1eSjsing 		.type = OPTION_FLAG,
193e7718adaStb 		.opt.flag = &cfg.rev,
19494e38b1eSjsing 	},
19594e38b1eSjsing 	{
19694e38b1eSjsing 		.name = "sign",
19794e38b1eSjsing 		.desc = "Sign the input data using RSA private key",
19894e38b1eSjsing 		.type = OPTION_VALUE,
19994e38b1eSjsing 		.value = RSA_SIGN,
200e7718adaStb 		.opt.value = &cfg.rsa_mode,
20194e38b1eSjsing 	},
20294e38b1eSjsing 	{
20394e38b1eSjsing 		.name = "verify",
20494e38b1eSjsing 		.desc = "Verify the input data using RSA public key",
20594e38b1eSjsing 		.type = OPTION_VALUE,
20694e38b1eSjsing 		.value = RSA_VERIFY,
207e7718adaStb 		.opt.value = &cfg.rsa_mode,
20894e38b1eSjsing 	},
209cec1ace2Stb 	{
210cec1ace2Stb 		.name = "x931",
211cec1ace2Stb 		.desc = "Use ANSI X9.31 padding",
212cec1ace2Stb 		.type = OPTION_VALUE,
213cec1ace2Stb 		.value = RSA_X931_PADDING,
214cec1ace2Stb 		.opt.value = &cfg.pad,
215cec1ace2Stb 	},
21694e38b1eSjsing 
21794e38b1eSjsing 	{NULL},
21894e38b1eSjsing };
21994e38b1eSjsing 
22094e38b1eSjsing static void
rsautl_usage(void)221*440d1414Stb rsautl_usage(void)
22294e38b1eSjsing {
22394e38b1eSjsing 	fprintf(stderr,
22494e38b1eSjsing 	    "usage: rsautl [-asn1parse] [-certin] [-decrypt] [-encrypt] "
22594e38b1eSjsing 	    "[-hexdump]\n"
22694e38b1eSjsing 	    "    [-in file] [-inkey file] [-keyform der | pem]\n"
227cec1ace2Stb 	    "    [-oaep | -pkcs | -raw | -x931] [-out file] [-passin arg]\n"
228ce730975Stb 	    "    [-pubin] [-rev] [-sign] [-verify]\n\n");
22994e38b1eSjsing 
23094e38b1eSjsing 	options_usage(rsautl_options);
23194e38b1eSjsing }
232dab3f910Sjsing 
233dab3f910Sjsing int
rsautl_main(int argc,char ** argv)234dab3f910Sjsing rsautl_main(int argc, char **argv)
235dab3f910Sjsing {
236dab3f910Sjsing 	BIO *in = NULL, *out = NULL;
237dab3f910Sjsing 	X509 *x;
238dab3f910Sjsing 	EVP_PKEY *pkey = NULL;
239dab3f910Sjsing 	RSA *rsa = NULL;
24094e38b1eSjsing 	unsigned char *rsa_in = NULL, *rsa_out = NULL;
24194e38b1eSjsing 	char *passin = NULL;
242dab3f910Sjsing 	int rsa_inlen, rsa_outlen = 0;
24394e38b1eSjsing 	int need_priv = 0;
244dab3f910Sjsing 	int keysize;
245dab3f910Sjsing 	int ret = 1;
246dab3f910Sjsing 
24751811eadSderaadt 	if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
2489bc487adSdoug 		perror("pledge");
249e370f0eeSdoug 		exit(1);
250e370f0eeSdoug 	}
2519bc487adSdoug 
252e7718adaStb 	memset(&cfg, 0, sizeof(cfg));
253e7718adaStb 	cfg.keyform = FORMAT_PEM;
254e7718adaStb 	cfg.key_type = KEY_PRIVKEY;
255e7718adaStb 	cfg.pad = RSA_PKCS1_PADDING;
256e7718adaStb 	cfg.rsa_mode = RSA_VERIFY;
257dab3f910Sjsing 
25894e38b1eSjsing 	if (options_parse(argc, argv, rsautl_options, NULL, NULL) != 0) {
25994e38b1eSjsing 		rsautl_usage();
26094e38b1eSjsing 		return (1);
261dab3f910Sjsing 	}
262dab3f910Sjsing 
263e7718adaStb 	if (cfg.rsa_mode == RSA_SIGN ||
264e7718adaStb 	    cfg.rsa_mode == RSA_DECRYPT)
26594e38b1eSjsing 		need_priv = 1;
26694e38b1eSjsing 
267e7718adaStb 	if (need_priv && cfg.key_type != KEY_PRIVKEY) {
268dab3f910Sjsing 		BIO_printf(bio_err, "A private key is needed for this operation\n");
269dab3f910Sjsing 		goto end;
270dab3f910Sjsing 	}
271e7718adaStb 	if (!app_passwd(bio_err, cfg.passargin, NULL, &passin, NULL)) {
272dab3f910Sjsing 		BIO_printf(bio_err, "Error getting password\n");
273dab3f910Sjsing 		goto end;
274dab3f910Sjsing 	}
275dab3f910Sjsing 
276e7718adaStb 	switch (cfg.key_type) {
277dab3f910Sjsing 	case KEY_PRIVKEY:
278e7718adaStb 		pkey = load_key(bio_err, cfg.keyfile,
279e7718adaStb 		    cfg.keyform, 0, passin, "Private Key");
280dab3f910Sjsing 		break;
281dab3f910Sjsing 
282dab3f910Sjsing 	case KEY_PUBKEY:
283e7718adaStb 		pkey = load_pubkey(bio_err, cfg.keyfile,
284e7718adaStb 		    cfg.keyform, 0, NULL, "Public Key");
285dab3f910Sjsing 		break;
286dab3f910Sjsing 
287dab3f910Sjsing 	case KEY_CERT:
288e7718adaStb 		x = load_cert(bio_err, cfg.keyfile,
289e7718adaStb 		    cfg.keyform, NULL, "Certificate");
290dab3f910Sjsing 		if (x) {
291dab3f910Sjsing 			pkey = X509_get_pubkey(x);
292dab3f910Sjsing 			X509_free(x);
293dab3f910Sjsing 		}
294dab3f910Sjsing 		break;
295dab3f910Sjsing 	}
296dab3f910Sjsing 
29799e8894fSdoug 	if (!pkey)
29899e8894fSdoug 		goto end;
29999e8894fSdoug 
300dab3f910Sjsing 	rsa = EVP_PKEY_get1_RSA(pkey);
301dab3f910Sjsing 	EVP_PKEY_free(pkey);
302dab3f910Sjsing 
303dab3f910Sjsing 	if (!rsa) {
304dab3f910Sjsing 		BIO_printf(bio_err, "Error getting RSA key\n");
305dab3f910Sjsing 		ERR_print_errors(bio_err);
306dab3f910Sjsing 		goto end;
307dab3f910Sjsing 	}
308e7718adaStb 	if (cfg.infile) {
309e7718adaStb 		if (!(in = BIO_new_file(cfg.infile, "rb"))) {
310dab3f910Sjsing 			BIO_printf(bio_err, "Error Reading Input File\n");
311dab3f910Sjsing 			ERR_print_errors(bio_err);
312dab3f910Sjsing 			goto end;
313dab3f910Sjsing 		}
314dab3f910Sjsing 	} else
315dab3f910Sjsing 		in = BIO_new_fp(stdin, BIO_NOCLOSE);
316dab3f910Sjsing 
317e7718adaStb 	if (cfg.outfile) {
318e7718adaStb 		if (!(out = BIO_new_file(cfg.outfile, "wb"))) {
319dab3f910Sjsing 			BIO_printf(bio_err, "Error Reading Output File\n");
320dab3f910Sjsing 			ERR_print_errors(bio_err);
321dab3f910Sjsing 			goto end;
322dab3f910Sjsing 		}
323dab3f910Sjsing 	} else {
324dab3f910Sjsing 		out = BIO_new_fp(stdout, BIO_NOCLOSE);
325dab3f910Sjsing 	}
326dab3f910Sjsing 
327dab3f910Sjsing 	keysize = RSA_size(rsa);
328dab3f910Sjsing 
329dab3f910Sjsing 	rsa_in = reallocarray(NULL, keysize, 2);
33040b191f1Slteo 	if (rsa_in == NULL) {
33140b191f1Slteo 		BIO_printf(bio_err, "Error allocating memory for input data\n");
33240b191f1Slteo 		exit(1);
33340b191f1Slteo 	}
334dab3f910Sjsing 	rsa_out = malloc(keysize);
33540b191f1Slteo 	if (rsa_out == NULL) {
33640b191f1Slteo 		BIO_printf(bio_err, "Error allocating memory for output data\n");
33740b191f1Slteo 		exit(1);
33840b191f1Slteo 	}
339dab3f910Sjsing 
340dab3f910Sjsing 	/* Read the input data */
341dab3f910Sjsing 	rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
342dab3f910Sjsing 	if (rsa_inlen <= 0) {
343dab3f910Sjsing 		BIO_printf(bio_err, "Error reading input Data\n");
344dab3f910Sjsing 		exit(1);
345dab3f910Sjsing 	}
346e7718adaStb 	if (cfg.rev) {
347dab3f910Sjsing 		int i;
348dab3f910Sjsing 		unsigned char ctmp;
349dab3f910Sjsing 		for (i = 0; i < rsa_inlen / 2; i++) {
350dab3f910Sjsing 			ctmp = rsa_in[i];
351dab3f910Sjsing 			rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
352dab3f910Sjsing 			rsa_in[rsa_inlen - 1 - i] = ctmp;
353dab3f910Sjsing 		}
354dab3f910Sjsing 	}
355dab3f910Sjsing 
356e7718adaStb 	switch (cfg.rsa_mode) {
357dab3f910Sjsing 	case RSA_VERIFY:
35894e38b1eSjsing 		rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out,
359e7718adaStb 		    rsa, cfg.pad);
360dab3f910Sjsing 		break;
361dab3f910Sjsing 
362dab3f910Sjsing 	case RSA_SIGN:
36394e38b1eSjsing 		rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out,
364e7718adaStb 		    rsa, cfg.pad);
365dab3f910Sjsing 		break;
366dab3f910Sjsing 
367dab3f910Sjsing 	case RSA_ENCRYPT:
36894e38b1eSjsing 		rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out,
369e7718adaStb 		    rsa, cfg.pad);
370dab3f910Sjsing 		break;
371dab3f910Sjsing 
372dab3f910Sjsing 	case RSA_DECRYPT:
37394e38b1eSjsing 		rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out,
374e7718adaStb 		    rsa, cfg.pad);
375dab3f910Sjsing 		break;
376dab3f910Sjsing 	}
377dab3f910Sjsing 
378dab3f910Sjsing 	if (rsa_outlen <= 0) {
379dab3f910Sjsing 		BIO_printf(bio_err, "RSA operation error\n");
380dab3f910Sjsing 		ERR_print_errors(bio_err);
381dab3f910Sjsing 		goto end;
382dab3f910Sjsing 	}
383dab3f910Sjsing 	ret = 0;
384e7718adaStb 	if (cfg.asn1parse) {
385dab3f910Sjsing 		if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
386dab3f910Sjsing 			ERR_print_errors(bio_err);
387dab3f910Sjsing 		}
388e7718adaStb 	} else if (cfg.hexdump)
389dab3f910Sjsing 		BIO_dump(out, (char *) rsa_out, rsa_outlen);
390dab3f910Sjsing 	else
391dab3f910Sjsing 		BIO_write(out, rsa_out, rsa_outlen);
392dab3f910Sjsing 
393dab3f910Sjsing  end:
394dab3f910Sjsing 	RSA_free(rsa);
395dab3f910Sjsing 	BIO_free(in);
396dab3f910Sjsing 	BIO_free_all(out);
397dab3f910Sjsing 	free(rsa_in);
398dab3f910Sjsing 	free(rsa_out);
399dab3f910Sjsing 	free(passin);
400dab3f910Sjsing 
401dab3f910Sjsing 	return ret;
402dab3f910Sjsing }
403