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