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