1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 /* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/err.h>
62 #include <openssl/pem.h>
63 #include <openssl/evp.h>
69 static void usage(void);
73 #define PROG pkeyutl_main
75 static EVP_PKEY_CTX
*init_ctx(int *pkeysize
,
76 char *keyfile
, int keyform
, int key_type
,
77 char *passargin
, int pkey_op
, ENGINE
*e
);
79 static int setup_peer(BIO
*err
, EVP_PKEY_CTX
*ctx
, int peerform
,
82 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
83 unsigned char *out
, size_t *poutlen
,
84 unsigned char *in
, size_t inlen
);
86 int MAIN(int argc
, char **);
88 int MAIN(int argc
, char **argv
)
90 BIO
*in
= NULL
, *out
= NULL
;
91 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
;
93 int pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
94 int keyform
= FORMAT_PEM
, peerform
= FORMAT_PEM
;
95 char badarg
= 0, rev
= 0;
96 char hexdump
= 0, asn1parse
= 0;
97 EVP_PKEY_CTX
*ctx
= NULL
;
98 char *passargin
= NULL
;
101 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
103 int buf_inlen
= 0, siglen
= -1;
105 int ret
= 1, rv
= -1;
110 if(!bio_err
) bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
112 if (!load_config(bio_err
, NULL
))
114 ERR_load_crypto_strings();
115 OpenSSL_add_all_algorithms();
119 if (!strcmp(*argv
,"-in"))
121 if (--argc
< 1) badarg
= 1;
124 else if (!strcmp(*argv
,"-out"))
126 if (--argc
< 1) badarg
= 1;
129 else if (!strcmp(*argv
,"-sigfile"))
131 if (--argc
< 1) badarg
= 1;
134 else if(!strcmp(*argv
, "-inkey"))
140 ctx
= init_ctx(&keysize
,
141 *(++argv
), keyform
, key_type
,
142 passargin
, pkey_op
, e
);
146 "Error initializing context\n");
147 ERR_print_errors(bio_err
);
152 else if (!strcmp(*argv
,"-peerkey"))
156 else if (!setup_peer(bio_err
, ctx
, peerform
, *(++argv
)))
159 else if (!strcmp(*argv
,"-passin"))
161 if (--argc
< 1) badarg
= 1;
162 passargin
= *(++argv
);
164 else if (strcmp(*argv
,"-peerform") == 0)
166 if (--argc
< 1) badarg
= 1;
167 peerform
=str2fmt(*(++argv
));
169 else if (strcmp(*argv
,"-keyform") == 0)
171 if (--argc
< 1) badarg
= 1;
172 keyform
=str2fmt(*(++argv
));
174 #ifndef OPENSSL_NO_ENGINE
175 else if(!strcmp(*argv
, "-engine"))
180 e
= setup_engine(bio_err
, *(++argv
), 0);
183 else if(!strcmp(*argv
, "-pubin"))
184 key_type
= KEY_PUBKEY
;
185 else if(!strcmp(*argv
, "-certin"))
187 else if(!strcmp(*argv
, "-asn1parse"))
189 else if(!strcmp(*argv
, "-hexdump"))
191 else if(!strcmp(*argv
, "-sign"))
192 pkey_op
= EVP_PKEY_OP_SIGN
;
193 else if(!strcmp(*argv
, "-verify"))
194 pkey_op
= EVP_PKEY_OP_VERIFY
;
195 else if(!strcmp(*argv
, "-verifyrecover"))
196 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
197 else if(!strcmp(*argv
, "-rev"))
199 else if(!strcmp(*argv
, "-encrypt"))
200 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
201 else if(!strcmp(*argv
, "-decrypt"))
202 pkey_op
= EVP_PKEY_OP_DECRYPT
;
203 else if(!strcmp(*argv
, "-derive"))
204 pkey_op
= EVP_PKEY_OP_DERIVE
;
205 else if (strcmp(*argv
,"-pkeyopt") == 0)
212 "-pkeyopt command before -inkey\n");
215 else if (pkey_ctrl_string(ctx
, *(++argv
)) <= 0)
217 BIO_puts(bio_err
, "parameter setting error\n");
218 ERR_print_errors(bio_err
);
238 if (sigfile
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
))
240 BIO_puts(bio_err
, "Signature file specified for non verify\n");
244 if (!sigfile
&& (pkey_op
== EVP_PKEY_OP_VERIFY
))
246 BIO_puts(bio_err
, "No signature file specified for verify\n");
250 /* FIXME: seed PRNG only if needed */
251 app_RAND_load_file(NULL
, bio_err
, 0);
253 if (pkey_op
!= EVP_PKEY_OP_DERIVE
)
257 if(!(in
= BIO_new_file(infile
, "rb")))
260 "Error Opening Input File\n");
261 ERR_print_errors(bio_err
);
266 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
271 if(!(out
= BIO_new_file(outfile
, "wb")))
273 BIO_printf(bio_err
, "Error Creating Output File\n");
274 ERR_print_errors(bio_err
);
280 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
281 #ifdef OPENSSL_SYS_VMS
283 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
284 out
= BIO_push(tmpbio
, out
);
291 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
294 BIO_printf(bio_err
, "Can't open signature file %s\n",
298 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
302 BIO_printf(bio_err
, "Error reading signature data\n");
309 /* Read the input data */
310 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
313 BIO_printf(bio_err
, "Error reading input Data\n");
320 size_t l
= (size_t)buf_inlen
;
321 for(i
= 0; i
< l
/2; i
++)
324 buf_in
[i
] = buf_in
[l
- 1 - i
];
325 buf_in
[l
- 1 - i
] = ctmp
;
330 if(pkey_op
== EVP_PKEY_OP_VERIFY
)
332 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
333 buf_in
, (size_t)buf_inlen
);
335 BIO_puts(out
, "Signature Verification Failure\n");
337 BIO_puts(out
, "Signature Verified Successfully\n");
343 rv
= do_keyop(ctx
, pkey_op
, NULL
, (size_t *)&buf_outlen
,
344 buf_in
, (size_t)buf_inlen
);
347 buf_out
= OPENSSL_malloc(buf_outlen
);
351 rv
= do_keyop(ctx
, pkey_op
,
352 buf_out
, (size_t *)&buf_outlen
,
353 buf_in
, (size_t)buf_inlen
);
359 BIO_printf(bio_err
, "Public Key operation error\n");
360 ERR_print_errors(bio_err
);
366 if(!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
367 ERR_print_errors(bio_err
);
370 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
372 BIO_write(out
, buf_out
, buf_outlen
);
376 EVP_PKEY_CTX_free(ctx
);
380 OPENSSL_free(buf_in
);
382 OPENSSL_free(buf_out
);
390 BIO_printf(bio_err
, "Usage: pkeyutl [options]\n");
391 BIO_printf(bio_err
, "-in file input file\n");
392 BIO_printf(bio_err
, "-out file output file\n");
393 BIO_printf(bio_err
, "-sigfile file signature file (verify operation only)\n");
394 BIO_printf(bio_err
, "-inkey file input key\n");
395 BIO_printf(bio_err
, "-keyform arg private key format - default PEM\n");
396 BIO_printf(bio_err
, "-pubin input is a public key\n");
397 BIO_printf(bio_err
, "-certin input is a certificate carrying a public key\n");
398 BIO_printf(bio_err
, "-pkeyopt X:Y public key options\n");
399 BIO_printf(bio_err
, "-sign sign with private key\n");
400 BIO_printf(bio_err
, "-verify verify with public key\n");
401 BIO_printf(bio_err
, "-verifyrecover verify with public key, recover original data\n");
402 BIO_printf(bio_err
, "-encrypt encrypt with public key\n");
403 BIO_printf(bio_err
, "-decrypt decrypt with private key\n");
404 BIO_printf(bio_err
, "-derive derive shared secret\n");
405 BIO_printf(bio_err
, "-hexdump hex dump output\n");
406 #ifndef OPENSSL_NO_ENGINE
407 BIO_printf(bio_err
, "-engine e use engine e, possibly a hardware device.\n");
409 BIO_printf(bio_err
, "-passin arg pass phrase source\n");
413 static EVP_PKEY_CTX
*init_ctx(int *pkeysize
,
414 char *keyfile
, int keyform
, int key_type
,
415 char *passargin
, int pkey_op
, ENGINE
*e
)
417 EVP_PKEY
*pkey
= NULL
;
418 EVP_PKEY_CTX
*ctx
= NULL
;
422 if(((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
423 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
424 && (key_type
!= KEY_PRIVKEY
))
426 BIO_printf(bio_err
, "A private key is needed for this operation\n");
429 if(!app_passwd(bio_err
, passargin
, NULL
, &passin
, NULL
))
431 BIO_printf(bio_err
, "Error getting password\n");
437 pkey
= load_key(bio_err
, keyfile
, keyform
, 0,
438 passin
, e
, "Private Key");
442 pkey
= load_pubkey(bio_err
, keyfile
, keyform
, 0,
443 NULL
, e
, "Public Key");
447 x
= load_cert(bio_err
, keyfile
, keyform
,
448 NULL
, e
, "Certificate");
451 pkey
= X509_get_pubkey(x
);
458 *pkeysize
= EVP_PKEY_size(pkey
);
463 ctx
= EVP_PKEY_CTX_new(pkey
, e
);
472 case EVP_PKEY_OP_SIGN
:
473 rv
= EVP_PKEY_sign_init(ctx
);
476 case EVP_PKEY_OP_VERIFY
:
477 rv
= EVP_PKEY_verify_init(ctx
);
480 case EVP_PKEY_OP_VERIFYRECOVER
:
481 rv
= EVP_PKEY_verify_recover_init(ctx
);
484 case EVP_PKEY_OP_ENCRYPT
:
485 rv
= EVP_PKEY_encrypt_init(ctx
);
488 case EVP_PKEY_OP_DECRYPT
:
489 rv
= EVP_PKEY_decrypt_init(ctx
);
492 case EVP_PKEY_OP_DERIVE
:
493 rv
= EVP_PKEY_derive_init(ctx
);
499 EVP_PKEY_CTX_free(ctx
);
506 OPENSSL_free(passin
);
513 static int setup_peer(BIO
*err
, EVP_PKEY_CTX
*ctx
, int peerform
,
516 EVP_PKEY
*peer
= NULL
;
520 BIO_puts(err
, "-peerkey command before -inkey\n");
524 peer
= load_pubkey(bio_err
, file
, peerform
, 0, NULL
, NULL
, "Peer Key");
528 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
529 ERR_print_errors(err
);
533 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
537 ERR_print_errors(err
);
541 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
542 unsigned char *out
, size_t *poutlen
,
543 unsigned char *in
, size_t inlen
)
548 case EVP_PKEY_OP_VERIFYRECOVER
:
549 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
552 case EVP_PKEY_OP_SIGN
:
553 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
556 case EVP_PKEY_OP_ENCRYPT
:
557 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
560 case EVP_PKEY_OP_DECRYPT
:
561 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
564 case EVP_PKEY_OP_DERIVE
:
565 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);