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