1 /* $OpenBSD: pkeyutl.c,v 1.16 2019/07/14 03:30:46 guenther 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>
87 static void pkeyutl_usage(void);
89 static int init_ctx(char *keyfile);
91 static int setup_peer(char *file);
93 static int pkeyutl_pkeyopt(char *pkeyopt);
95 static int do_keyop(EVP_PKEY_CTX * ctx, int pkey_op,
96 unsigned char *out, size_t * poutlen,
97 unsigned char *in, size_t inlen);
99 static const struct option pkeyutl_options[] = {
102 .desc = "ASN.1 parse the output data",
104 .opt.flag = &pkeyutl_config.asn1parse,
108 .desc = "Input is a certificate containing a public key",
109 .type = OPTION_VALUE,
111 .opt.value = &pkeyutl_config.key_type,
115 .desc = "Decrypt the input data using a private key",
116 .type = OPTION_VALUE,
117 .value = EVP_PKEY_OP_DECRYPT,
118 .opt.value = &pkeyutl_config.pkey_op,
122 .desc = "Derive a shared secret using the peer key",
123 .type = OPTION_VALUE,
124 .value = EVP_PKEY_OP_DERIVE,
125 .opt.value = &pkeyutl_config.pkey_op,
129 .desc = "Encrypt the input data using a public key",
130 .type = OPTION_VALUE,
131 .value = EVP_PKEY_OP_ENCRYPT,
132 .opt.value = &pkeyutl_config.pkey_op,
136 .desc = "Hex dump the output data",
138 .opt.flag = &pkeyutl_config.hexdump,
143 .desc = "Input file (default stdin)",
145 .opt.arg = &pkeyutl_config.infile,
150 .desc = "Input key file",
151 .type = OPTION_ARG_FUNC,
152 .opt.argfunc = init_ctx,
157 .desc = "Input key format (DER or PEM (default))",
158 .type = OPTION_ARG_FORMAT,
159 .opt.value = &pkeyutl_config.keyform,
164 .desc = "Output file (default stdout)",
166 .opt.arg = &pkeyutl_config.outfile,
171 .desc = "Key password source",
173 .opt.arg = &pkeyutl_config.passargin,
178 .desc = "Input key format (DER or PEM (default))",
179 .type = OPTION_ARG_FORMAT,
180 .opt.value = &pkeyutl_config.peerform,
185 .desc = "Peer key file",
186 .type = OPTION_ARG_FUNC,
187 .opt.argfunc = setup_peer,
191 .argname = "opt:value",
192 .desc = "Public key options",
193 .type = OPTION_ARG_FUNC,
194 .opt.argfunc = pkeyutl_pkeyopt,
198 .desc = "Input is a public key",
199 .type = OPTION_VALUE,
201 .opt.value = &pkeyutl_config.key_type,
205 .desc = "Reverse the input data",
207 .opt.flag = &pkeyutl_config.rev,
212 .desc = "Signature file (verify operation only)",
214 .opt.arg = &pkeyutl_config.sigfile,
218 .desc = "Sign the input data using private key",
219 .type = OPTION_VALUE,
220 .value = EVP_PKEY_OP_SIGN,
221 .opt.value = &pkeyutl_config.pkey_op,
225 .desc = "Verify the input data using public key",
226 .type = OPTION_VALUE,
227 .value = EVP_PKEY_OP_VERIFY,
228 .opt.value = &pkeyutl_config.pkey_op,
231 .name = "verifyrecover",
232 .desc = "Verify with public key, recover original data",
233 .type = OPTION_VALUE,
234 .value = EVP_PKEY_OP_VERIFYRECOVER,
235 .opt.value = &pkeyutl_config.pkey_op,
245 "usage: pkeyutl [-asn1parse] [-certin] [-decrypt] [-derive] "
247 " [-hexdump] [-in file] [-inkey file] [-keyform fmt]\n"
248 " [-out file] [-passin arg] [-peerform fmt]\n"
249 " [-peerkey file] [-pkeyopt opt:value] [-pubin] [-rev]\n"
250 " [-sigfile file] [-sign] [-verify] [-verifyrecover]\n\n");
251 options_usage(pkeyutl_options);
252 fprintf(stderr, "\n");
256 pkeyutl_main(int argc, char **argv)
258 BIO *in = NULL, *out = NULL;
260 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
261 size_t buf_outlen = 0;
262 int buf_inlen = 0, siglen = -1;
264 int ret = 1, rv = -1;
266 if (single_execution) {
267 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
273 memset(&pkeyutl_config, 0, sizeof(pkeyutl_config));
274 pkeyutl_config.pkey_op = EVP_PKEY_OP_SIGN;
275 pkeyutl_config.key_type = KEY_PRIVKEY;
276 pkeyutl_config.keyform = FORMAT_PEM;
277 pkeyutl_config.peerform = FORMAT_PEM;
278 pkeyutl_config.keysize = -1;
280 if (options_parse(argc, argv, pkeyutl_options, NULL, NULL) != 0) {
285 if (!pkeyutl_config.ctx) {
289 if (pkeyutl_config.sigfile &&
290 (pkeyutl_config.pkey_op != EVP_PKEY_OP_VERIFY)) {
291 BIO_puts(bio_err, "Signature file specified for non verify\n");
294 if (!pkeyutl_config.sigfile &&
295 (pkeyutl_config.pkey_op == EVP_PKEY_OP_VERIFY)) {
296 BIO_puts(bio_err, "No signature file specified for verify\n");
300 if (pkeyutl_config.pkey_op != EVP_PKEY_OP_DERIVE) {
301 if (pkeyutl_config.infile) {
302 if (!(in = BIO_new_file(pkeyutl_config.infile, "rb"))) {
304 "Error Opening Input File\n");
305 ERR_print_errors(bio_err);
309 in = BIO_new_fp(stdin, BIO_NOCLOSE);
311 if (pkeyutl_config.outfile) {
312 if (!(out = BIO_new_file(pkeyutl_config.outfile, "wb"))) {
313 BIO_printf(bio_err, "Error Creating Output File\n");
314 ERR_print_errors(bio_err);
318 out = BIO_new_fp(stdout, BIO_NOCLOSE);
321 if (pkeyutl_config.sigfile) {
322 BIO *sigbio = BIO_new_file(pkeyutl_config.sigfile, "rb");
324 BIO_printf(bio_err, "Can't open signature file %s\n",
325 pkeyutl_config.sigfile);
328 siglen = bio_to_mem(&sig, pkeyutl_config.keysize * 10, sigbio);
331 BIO_printf(bio_err, "Error reading signature data\n");
336 /* Read the input data */
337 buf_inlen = bio_to_mem(&buf_in, pkeyutl_config.keysize * 10, in);
338 if (buf_inlen <= 0) {
339 BIO_printf(bio_err, "Error reading input Data\n");
342 if (pkeyutl_config.rev) {
345 size_t l = (size_t) buf_inlen;
346 for (i = 0; i < l / 2; i++) {
348 buf_in[i] = buf_in[l - 1 - i];
349 buf_in[l - 1 - i] = ctmp;
353 if (pkeyutl_config.pkey_op == EVP_PKEY_OP_VERIFY) {
354 rv = EVP_PKEY_verify(pkeyutl_config.ctx, sig, (size_t) siglen,
355 buf_in, (size_t) buf_inlen);
357 BIO_puts(out, "Signature Verified Successfully\n");
360 BIO_puts(out, "Signature Verification Failure\n");
364 rv = do_keyop(pkeyutl_config.ctx, pkeyutl_config.pkey_op, NULL,
365 (size_t *)&buf_outlen, buf_in, (size_t) buf_inlen);
367 buf_out = malloc(buf_outlen);
371 rv = do_keyop(pkeyutl_config.ctx,
372 pkeyutl_config.pkey_op,
373 buf_out, (size_t *) & buf_outlen,
374 buf_in, (size_t) buf_inlen);
379 BIO_printf(bio_err, "Public Key operation error\n");
380 ERR_print_errors(bio_err);
384 if (pkeyutl_config.asn1parse) {
385 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
386 ERR_print_errors(bio_err);
387 } else if (pkeyutl_config.hexdump)
388 BIO_dump(out, (char *) buf_out, buf_outlen);
390 BIO_write(out, buf_out, buf_outlen);
393 EVP_PKEY_CTX_free(pkeyutl_config.ctx);
404 init_ctx(char *keyfile)
406 EVP_PKEY *pkey = NULL;
411 if (((pkeyutl_config.pkey_op == EVP_PKEY_OP_SIGN)
412 || (pkeyutl_config.pkey_op == EVP_PKEY_OP_DECRYPT)
413 || (pkeyutl_config.pkey_op == EVP_PKEY_OP_DERIVE))
414 && (pkeyutl_config.key_type != KEY_PRIVKEY)) {
416 "A private key is needed for this operation\n");
419 if (!app_passwd(bio_err, pkeyutl_config.passargin, NULL, &passin,
421 BIO_printf(bio_err, "Error getting password\n");
424 switch (pkeyutl_config.key_type) {
426 pkey = load_key(bio_err, keyfile, pkeyutl_config.keyform, 0,
427 passin, "Private Key");
431 pkey = load_pubkey(bio_err, keyfile, pkeyutl_config.keyform, 0,
436 x = load_cert(bio_err, keyfile, pkeyutl_config.keyform,
437 NULL, "Certificate");
439 pkey = X509_get_pubkey(x);
445 pkeyutl_config.keysize = EVP_PKEY_size(pkey);
450 pkeyutl_config.ctx = EVP_PKEY_CTX_new(pkey, NULL);
454 if (!pkeyutl_config.ctx)
457 switch (pkeyutl_config.pkey_op) {
458 case EVP_PKEY_OP_SIGN:
459 rv = EVP_PKEY_sign_init(pkeyutl_config.ctx);
462 case EVP_PKEY_OP_VERIFY:
463 rv = EVP_PKEY_verify_init(pkeyutl_config.ctx);
466 case EVP_PKEY_OP_VERIFYRECOVER:
467 rv = EVP_PKEY_verify_recover_init(pkeyutl_config.ctx);
470 case EVP_PKEY_OP_ENCRYPT:
471 rv = EVP_PKEY_encrypt_init(pkeyutl_config.ctx);
474 case EVP_PKEY_OP_DECRYPT:
475 rv = EVP_PKEY_decrypt_init(pkeyutl_config.ctx);
478 case EVP_PKEY_OP_DERIVE:
479 rv = EVP_PKEY_derive_init(pkeyutl_config.ctx);
484 EVP_PKEY_CTX_free(pkeyutl_config.ctx);
485 pkeyutl_config.ctx = NULL;
491 if (!pkeyutl_config.ctx) {
492 BIO_puts(bio_err, "Error initializing context\n");
493 ERR_print_errors(bio_err);
501 setup_peer(char *file)
503 EVP_PKEY *peer = NULL;
506 if (!pkeyutl_config.ctx) {
507 BIO_puts(bio_err, "-peerkey command before -inkey\n");
510 peer = load_pubkey(bio_err, file, pkeyutl_config.peerform, 0, NULL,
514 BIO_printf(bio_err, "Error reading peer key %s\n", file);
515 ERR_print_errors(bio_err);
518 ret = EVP_PKEY_derive_set_peer(pkeyutl_config.ctx, peer);
522 ERR_print_errors(bio_err);
530 pkeyutl_pkeyopt(char *pkeyopt)
532 if (!pkeyutl_config.ctx) {
533 BIO_puts(bio_err, "-pkeyopt command before -inkey\n");
535 } else if (pkey_ctrl_string(pkeyutl_config.ctx, pkeyopt) <= 0) {
536 BIO_puts(bio_err, "parameter setting error\n");
537 ERR_print_errors(bio_err);
545 do_keyop(EVP_PKEY_CTX * ctx, int pkey_op,
546 unsigned char *out, size_t * poutlen,
547 unsigned char *in, size_t inlen)
551 case EVP_PKEY_OP_VERIFYRECOVER:
552 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
555 case EVP_PKEY_OP_SIGN:
556 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
559 case EVP_PKEY_OP_ENCRYPT:
560 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
563 case EVP_PKEY_OP_DECRYPT:
564 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
567 case EVP_PKEY_OP_DERIVE:
568 rv = EVP_PKEY_derive(ctx, out, poutlen);