1 /* $OpenBSD: pkeyutl.c,v 1.14 2018/02/07 05:47:55 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/err.h>
64 #include <openssl/evp.h>
65 #include <openssl/pem.h>
71 static void usage(void);
73 static EVP_PKEY_CTX
*init_ctx(int *pkeysize
,
74 char *keyfile
, int keyform
, int key_type
,
75 char *passargin
, int pkey_op
);
77 static int setup_peer(BIO
* err
, EVP_PKEY_CTX
* ctx
, int peerform
,
80 static int do_keyop(EVP_PKEY_CTX
* ctx
, int pkey_op
,
81 unsigned char *out
, size_t * poutlen
,
82 unsigned char *in
, size_t inlen
);
85 pkeyutl_main(int argc
, char **argv
)
87 BIO
*in
= NULL
, *out
= NULL
;
88 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
;
89 int pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
90 int keyform
= FORMAT_PEM
, peerform
= FORMAT_PEM
;
91 char badarg
= 0, rev
= 0;
92 char hexdump
= 0, asn1parse
= 0;
93 EVP_PKEY_CTX
*ctx
= NULL
;
94 char *passargin
= NULL
;
97 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
98 size_t buf_outlen
= 0;
99 int buf_inlen
= 0, siglen
= -1;
101 int ret
= 1, rv
= -1;
103 if (single_execution
) {
104 if (pledge("stdio cpath wpath rpath tty", NULL
) == -1) {
114 if (!strcmp(*argv
, "-in")) {
119 } else if (!strcmp(*argv
, "-out")) {
124 } else if (!strcmp(*argv
, "-sigfile")) {
129 } else if (!strcmp(*argv
, "-inkey")) {
133 ctx
= init_ctx(&keysize
,
134 *(++argv
), keyform
, key_type
,
138 "Error initializing context\n");
139 ERR_print_errors(bio_err
);
143 } else if (!strcmp(*argv
, "-peerkey")) {
146 else if (!setup_peer(bio_err
, ctx
, peerform
, *(++argv
)))
148 } else if (!strcmp(*argv
, "-passin")) {
152 passargin
= *(++argv
);
153 } else if (strcmp(*argv
, "-peerform") == 0) {
157 peerform
= str2fmt(*(++argv
));
158 } else if (strcmp(*argv
, "-keyform") == 0) {
162 keyform
= str2fmt(*(++argv
));
164 else if (!strcmp(*argv
, "-pubin"))
165 key_type
= KEY_PUBKEY
;
166 else if (!strcmp(*argv
, "-certin"))
168 else if (!strcmp(*argv
, "-asn1parse"))
170 else if (!strcmp(*argv
, "-hexdump"))
172 else if (!strcmp(*argv
, "-sign"))
173 pkey_op
= EVP_PKEY_OP_SIGN
;
174 else if (!strcmp(*argv
, "-verify"))
175 pkey_op
= EVP_PKEY_OP_VERIFY
;
176 else if (!strcmp(*argv
, "-verifyrecover"))
177 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
178 else if (!strcmp(*argv
, "-rev"))
180 else if (!strcmp(*argv
, "-encrypt"))
181 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
182 else if (!strcmp(*argv
, "-decrypt"))
183 pkey_op
= EVP_PKEY_OP_DECRYPT
;
184 else if (!strcmp(*argv
, "-derive"))
185 pkey_op
= EVP_PKEY_OP_DERIVE
;
186 else if (strcmp(*argv
, "-pkeyopt") == 0) {
191 "-pkeyopt command before -inkey\n");
193 } else if (pkey_ctrl_string(ctx
, *(++argv
)) <= 0) {
194 BIO_puts(bio_err
, "parameter setting error\n");
195 ERR_print_errors(bio_err
);
212 if (sigfile
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
213 BIO_puts(bio_err
, "Signature file specified for non verify\n");
216 if (!sigfile
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
217 BIO_puts(bio_err
, "No signature file specified for verify\n");
221 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
223 if (!(in
= BIO_new_file(infile
, "rb"))) {
225 "Error Opening Input File\n");
226 ERR_print_errors(bio_err
);
230 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
233 if (!(out
= BIO_new_file(outfile
, "wb"))) {
234 BIO_printf(bio_err
, "Error Creating Output File\n");
235 ERR_print_errors(bio_err
);
239 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
243 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
245 BIO_printf(bio_err
, "Can't open signature file %s\n",
249 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
252 BIO_printf(bio_err
, "Error reading signature data\n");
257 /* Read the input data */
258 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
259 if (buf_inlen
<= 0) {
260 BIO_printf(bio_err
, "Error reading input Data\n");
266 size_t l
= (size_t) buf_inlen
;
267 for (i
= 0; i
< l
/ 2; i
++) {
269 buf_in
[i
] = buf_in
[l
- 1 - i
];
270 buf_in
[l
- 1 - i
] = ctmp
;
274 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
275 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t) siglen
,
276 buf_in
, (size_t) buf_inlen
);
278 BIO_puts(out
, "Signature Verified Successfully\n");
281 BIO_puts(out
, "Signature Verification Failure\n");
285 rv
= do_keyop(ctx
, pkey_op
, NULL
, (size_t *)&buf_outlen
,
286 buf_in
, (size_t) buf_inlen
);
288 buf_out
= malloc(buf_outlen
);
292 rv
= do_keyop(ctx
, pkey_op
,
293 buf_out
, (size_t *) & buf_outlen
,
294 buf_in
, (size_t) buf_inlen
);
299 BIO_printf(bio_err
, "Public Key operation error\n");
300 ERR_print_errors(bio_err
);
305 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
306 ERR_print_errors(bio_err
);
308 BIO_dump(out
, (char *) buf_out
, buf_outlen
);
310 BIO_write(out
, buf_out
, buf_outlen
);
313 EVP_PKEY_CTX_free(ctx
);
326 BIO_printf(bio_err
, "Usage: pkeyutl [options]\n");
327 BIO_printf(bio_err
, "-in file input file\n");
328 BIO_printf(bio_err
, "-out file output file\n");
329 BIO_printf(bio_err
, "-sigfile file signature file (verify operation only)\n");
330 BIO_printf(bio_err
, "-inkey file input key\n");
331 BIO_printf(bio_err
, "-keyform arg private key format - default PEM\n");
332 BIO_printf(bio_err
, "-pubin input is a public key\n");
333 BIO_printf(bio_err
, "-certin input is a certificate carrying a public key\n");
334 BIO_printf(bio_err
, "-pkeyopt X:Y public key options\n");
335 BIO_printf(bio_err
, "-sign sign with private key\n");
336 BIO_printf(bio_err
, "-verify verify with public key\n");
337 BIO_printf(bio_err
, "-verifyrecover verify with public key, recover original data\n");
338 BIO_printf(bio_err
, "-encrypt encrypt with public key\n");
339 BIO_printf(bio_err
, "-decrypt decrypt with private key\n");
340 BIO_printf(bio_err
, "-derive derive shared secret\n");
341 BIO_printf(bio_err
, "-hexdump hex dump output\n");
342 BIO_printf(bio_err
, "-passin arg pass phrase source\n");
346 static EVP_PKEY_CTX
*
347 init_ctx(int *pkeysize
,
348 char *keyfile
, int keyform
, int key_type
,
349 char *passargin
, int pkey_op
)
351 EVP_PKEY
*pkey
= NULL
;
352 EVP_PKEY_CTX
*ctx
= NULL
;
356 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
357 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
358 && (key_type
!= KEY_PRIVKEY
)) {
359 BIO_printf(bio_err
, "A private key is needed for this operation\n");
362 if (!app_passwd(bio_err
, passargin
, NULL
, &passin
, NULL
)) {
363 BIO_printf(bio_err
, "Error getting password\n");
368 pkey
= load_key(bio_err
, keyfile
, keyform
, 0,
369 passin
, "Private Key");
373 pkey
= load_pubkey(bio_err
, keyfile
, keyform
, 0,
378 x
= load_cert(bio_err
, keyfile
, keyform
,
379 NULL
, "Certificate");
381 pkey
= X509_get_pubkey(x
);
388 *pkeysize
= EVP_PKEY_size(pkey
);
393 ctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
401 case EVP_PKEY_OP_SIGN
:
402 rv
= EVP_PKEY_sign_init(ctx
);
405 case EVP_PKEY_OP_VERIFY
:
406 rv
= EVP_PKEY_verify_init(ctx
);
409 case EVP_PKEY_OP_VERIFYRECOVER
:
410 rv
= EVP_PKEY_verify_recover_init(ctx
);
413 case EVP_PKEY_OP_ENCRYPT
:
414 rv
= EVP_PKEY_encrypt_init(ctx
);
417 case EVP_PKEY_OP_DECRYPT
:
418 rv
= EVP_PKEY_decrypt_init(ctx
);
421 case EVP_PKEY_OP_DERIVE
:
422 rv
= EVP_PKEY_derive_init(ctx
);
427 EVP_PKEY_CTX_free(ctx
);
440 setup_peer(BIO
* err
, EVP_PKEY_CTX
* ctx
, int peerform
,
443 EVP_PKEY
*peer
= NULL
;
446 BIO_puts(err
, "-peerkey command before -inkey\n");
449 peer
= load_pubkey(bio_err
, file
, peerform
, 0, NULL
, "Peer Key");
452 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
453 ERR_print_errors(err
);
456 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
460 ERR_print_errors(err
);
465 do_keyop(EVP_PKEY_CTX
* ctx
, int pkey_op
,
466 unsigned char *out
, size_t * poutlen
,
467 unsigned char *in
, size_t inlen
)
471 case EVP_PKEY_OP_VERIFYRECOVER
:
472 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
475 case EVP_PKEY_OP_SIGN
:
476 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
479 case EVP_PKEY_OP_ENCRYPT
:
480 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
483 case EVP_PKEY_OP_DECRYPT
:
484 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
487 case EVP_PKEY_OP_DERIVE
:
488 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);