2 * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
17 # include <openssl/err.h>
18 # include <openssl/pem.h>
19 # include <openssl/rsa.h>
23 # define RSA_ENCRYPT 3
24 # define RSA_DECRYPT 4
26 # define KEY_PRIVKEY 1
30 typedef 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
,
39 const OPTIONS rsautl_options
[] = {
40 {"help", OPT_HELP
, '-', "Display this summary"},
41 {"in", OPT_IN
, '<', "Input file"},
42 {"out", OPT_OUT
, '>', "Output file"},
43 {"inkey", OPT_INKEY
, 's', "Input key"},
44 {"keyform", OPT_KEYFORM
, 'E', "Private key format - default PEM"},
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"},
53 {"asn1parse", OPT_ASN1PARSE
, '-',
54 "Run output through asn1parse; useful with -verify"},
55 {"hexdump", OPT_HEXDUMP
, '-', "Hex dump output"},
56 {"x931", OPT_X931
, '-', "Use ANSI X9.31 padding"},
57 {"rev", OPT_REV
, '-', "Reverse the order of the input buffer"},
58 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt with public key"},
59 {"decrypt", OPT_DECRYPT
, '-', "Decrypt with private key"},
60 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
62 # ifndef OPENSSL_NO_ENGINE
63 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
68 int rsautl_main(int argc
, char **argv
)
70 BIO
*in
= NULL
, *out
= NULL
;
72 EVP_PKEY
*pkey
= NULL
;
75 char *infile
= NULL
, *outfile
= NULL
, *keyfile
= NULL
;
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;
83 prog
= opt_init(argc
, argv
, rsautl_options
);
84 while ((o
= opt_next()) != OPT_EOF
) {
89 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
92 opt_help(rsautl_options
);
96 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &keyformat
))
106 e
= setup_engine(opt_arg(), 0);
115 pad
= RSA_NO_PADDING
;
118 pad
= RSA_PKCS1_OAEP_PADDING
;
121 pad
= RSA_SSLV23_PADDING
;
124 pad
= RSA_PKCS1_PADDING
;
127 pad
= RSA_X931_PADDING
;
134 rsa_mode
= RSA_VERIFY
;
140 rsa_mode
= RSA_ENCRYPT
;
143 rsa_mode
= RSA_DECRYPT
;
147 key_type
= KEY_PUBKEY
;
156 passinarg
= opt_arg();
164 argc
= opt_num_rest();
168 if (need_priv
&& (key_type
!= KEY_PRIVKEY
)) {
169 BIO_printf(bio_err
, "A private key is needed for this operation\n");
173 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
174 BIO_printf(bio_err
, "Error getting password\n");
180 pkey
= load_key(keyfile
, keyformat
, 0, passin
, e
, "Private Key");
184 pkey
= load_pubkey(keyfile
, keyformat
, 0, NULL
, e
, "Public Key");
188 x
= load_cert(keyfile
, keyformat
, "Certificate");
190 pkey
= X509_get_pubkey(x
);
199 rsa
= EVP_PKEY_get1_RSA(pkey
);
203 BIO_printf(bio_err
, "Error getting RSA key\n");
204 ERR_print_errors(bio_err
);
208 in
= bio_open_default(infile
, 'r', FORMAT_BINARY
);
211 out
= bio_open_default(outfile
, 'w', FORMAT_BINARY
);
215 keysize
= RSA_size(rsa
);
217 rsa_in
= app_malloc(keysize
* 2, "hold rsa key");
218 rsa_out
= app_malloc(keysize
, "output rsa key");
220 /* Read the input data */
221 rsa_inlen
= BIO_read(in
, rsa_in
, keysize
* 2);
223 BIO_printf(bio_err
, "Error reading input Data\n");
229 for (i
= 0; i
< rsa_inlen
/ 2; i
++) {
231 rsa_in
[i
] = rsa_in
[rsa_inlen
- 1 - i
];
232 rsa_in
[rsa_inlen
- 1 - i
] = ctmp
;
238 rsa_outlen
= RSA_public_decrypt(rsa_inlen
, rsa_in
, rsa_out
, rsa
, pad
);
243 RSA_private_encrypt(rsa_inlen
, rsa_in
, rsa_out
, rsa
, pad
);
247 rsa_outlen
= RSA_public_encrypt(rsa_inlen
, rsa_in
, rsa_out
, rsa
, pad
);
252 RSA_private_decrypt(rsa_inlen
, rsa_in
, rsa_out
, rsa
, pad
);
256 if (rsa_outlen
< 0) {
257 BIO_printf(bio_err
, "RSA operation error\n");
258 ERR_print_errors(bio_err
);
263 if (!ASN1_parse_dump(out
, rsa_out
, rsa_outlen
, 1, -1)) {
264 ERR_print_errors(bio_err
);
266 } else if (hexdump
) {
267 BIO_dump(out
, (char *)rsa_out
, rsa_outlen
);
269 BIO_write(out
, rsa_out
, rsa_outlen
);
276 OPENSSL_free(rsa_in
);
277 OPENSSL_free(rsa_out
);
278 OPENSSL_free(passin
);