Annotation of src/usr.bin/openssl/pkeyutl.c, Revision 1.8
1.8 ! doug 1: /* $OpenBSD: pkeyutl.c,v 1.7 2015/09/11 14:30:23 bcook Exp $ */
1.1 jsing 2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3: * project 2006.
4: */
5: /* ====================================================================
6: * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: *
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
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
18: * distribution.
19: *
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/)"
24: *
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.
29: *
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.
33: *
34: * 6. Redistributions of any form whatsoever must retain the following
35: * acknowledgment:
36: * "This product includes software developed by the OpenSSL Project
37: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38: *
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: * ====================================================================
52: *
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).
56: *
57: */
58:
59: #include <string.h>
60:
61: #include "apps.h"
62:
63: #include <openssl/err.h>
64: #include <openssl/evp.h>
65: #include <openssl/pem.h>
66:
67: #define KEY_PRIVKEY 1
68: #define KEY_PUBKEY 2
69: #define KEY_CERT 3
70:
71: static void usage(void);
72:
73: static EVP_PKEY_CTX *init_ctx(int *pkeysize,
74: char *keyfile, int keyform, int key_type,
1.7 bcook 75: char *passargin, int pkey_op);
1.1 jsing 76:
77: static int setup_peer(BIO * err, EVP_PKEY_CTX * ctx, int peerform,
78: const char *file);
79:
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);
83:
84: int
85: pkeyutl_main(int argc, char **argv)
86: {
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;
95: int keysize = -1;
96:
97: unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
98: size_t buf_outlen;
99: int buf_inlen = 0, siglen = -1;
100:
101: int ret = 1, rv = -1;
1.8 ! doug 102:
! 103: if (single_execution) {
! 104: if (pledge("stdio rpath wpath cpath", NULL) == -1)
! 105: perror("pledge");
! 106: }
1.1 jsing 107:
108: argc--;
109: argv++;
110:
111: while (argc >= 1) {
112: if (!strcmp(*argv, "-in")) {
113: if (--argc < 1)
114: badarg = 1;
115: else
116: infile = *(++argv);
117: } else if (!strcmp(*argv, "-out")) {
118: if (--argc < 1)
119: badarg = 1;
120: else
121: outfile = *(++argv);
122: } else if (!strcmp(*argv, "-sigfile")) {
123: if (--argc < 1)
124: badarg = 1;
125: else
126: sigfile = *(++argv);
127: } else if (!strcmp(*argv, "-inkey")) {
128: if (--argc < 1)
129: badarg = 1;
130: else {
131: ctx = init_ctx(&keysize,
132: *(++argv), keyform, key_type,
1.7 bcook 133: passargin, pkey_op);
1.1 jsing 134: if (!ctx) {
135: BIO_puts(bio_err,
136: "Error initializing context\n");
137: ERR_print_errors(bio_err);
138: badarg = 1;
139: }
140: }
141: } else if (!strcmp(*argv, "-peerkey")) {
142: if (--argc < 1)
143: badarg = 1;
144: else if (!setup_peer(bio_err, ctx, peerform, *(++argv)))
145: badarg = 1;
146: } else if (!strcmp(*argv, "-passin")) {
147: if (--argc < 1)
148: badarg = 1;
149: else
150: passargin = *(++argv);
151: } else if (strcmp(*argv, "-peerform") == 0) {
152: if (--argc < 1)
153: badarg = 1;
154: else
155: peerform = str2fmt(*(++argv));
156: } else if (strcmp(*argv, "-keyform") == 0) {
157: if (--argc < 1)
158: badarg = 1;
159: else
160: keyform = str2fmt(*(++argv));
161: }
162: else if (!strcmp(*argv, "-pubin"))
163: key_type = KEY_PUBKEY;
164: else if (!strcmp(*argv, "-certin"))
165: key_type = KEY_CERT;
166: else if (!strcmp(*argv, "-asn1parse"))
167: asn1parse = 1;
168: else if (!strcmp(*argv, "-hexdump"))
169: hexdump = 1;
170: else if (!strcmp(*argv, "-sign"))
171: pkey_op = EVP_PKEY_OP_SIGN;
172: else if (!strcmp(*argv, "-verify"))
173: pkey_op = EVP_PKEY_OP_VERIFY;
174: else if (!strcmp(*argv, "-verifyrecover"))
175: pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
176: else if (!strcmp(*argv, "-rev"))
177: rev = 1;
178: else if (!strcmp(*argv, "-encrypt"))
179: pkey_op = EVP_PKEY_OP_ENCRYPT;
180: else if (!strcmp(*argv, "-decrypt"))
181: pkey_op = EVP_PKEY_OP_DECRYPT;
182: else if (!strcmp(*argv, "-derive"))
183: pkey_op = EVP_PKEY_OP_DERIVE;
184: else if (strcmp(*argv, "-pkeyopt") == 0) {
185: if (--argc < 1)
186: badarg = 1;
187: else if (!ctx) {
188: BIO_puts(bio_err,
189: "-pkeyopt command before -inkey\n");
190: badarg = 1;
191: } else if (pkey_ctrl_string(ctx, *(++argv)) <= 0) {
192: BIO_puts(bio_err, "parameter setting error\n");
193: ERR_print_errors(bio_err);
194: goto end;
195: }
196: } else
197: badarg = 1;
198: if (badarg) {
199: usage();
200: goto end;
201: }
202: argc--;
203: argv++;
204: }
205:
206: if (!ctx) {
207: usage();
208: goto end;
209: }
210: if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
211: BIO_puts(bio_err, "Signature file specified for non verify\n");
212: goto end;
213: }
214: if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) {
215: BIO_puts(bio_err, "No signature file specified for verify\n");
216: goto end;
217: }
218:
219: if (pkey_op != EVP_PKEY_OP_DERIVE) {
220: if (infile) {
221: if (!(in = BIO_new_file(infile, "rb"))) {
222: BIO_puts(bio_err,
223: "Error Opening Input File\n");
224: ERR_print_errors(bio_err);
225: goto end;
226: }
227: } else
228: in = BIO_new_fp(stdin, BIO_NOCLOSE);
229: }
230: if (outfile) {
231: if (!(out = BIO_new_file(outfile, "wb"))) {
232: BIO_printf(bio_err, "Error Creating Output File\n");
233: ERR_print_errors(bio_err);
234: goto end;
235: }
236: } else {
237: out = BIO_new_fp(stdout, BIO_NOCLOSE);
238: }
239:
240: if (sigfile) {
241: BIO *sigbio = BIO_new_file(sigfile, "rb");
242: if (!sigbio) {
243: BIO_printf(bio_err, "Can't open signature file %s\n",
244: sigfile);
245: goto end;
246: }
247: siglen = bio_to_mem(&sig, keysize * 10, sigbio);
248: BIO_free(sigbio);
249: if (siglen <= 0) {
250: BIO_printf(bio_err, "Error reading signature data\n");
251: goto end;
252: }
253: }
254: if (in) {
255: /* Read the input data */
256: buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
257: if (buf_inlen <= 0) {
258: BIO_printf(bio_err, "Error reading input Data\n");
259: exit(1);
260: }
261: if (rev) {
262: size_t i;
263: unsigned char ctmp;
264: size_t l = (size_t) buf_inlen;
265: for (i = 0; i < l / 2; i++) {
266: ctmp = buf_in[i];
267: buf_in[i] = buf_in[l - 1 - i];
268: buf_in[l - 1 - i] = ctmp;
269: }
270: }
271: }
272: if (pkey_op == EVP_PKEY_OP_VERIFY) {
273: rv = EVP_PKEY_verify(ctx, sig, (size_t) siglen,
274: buf_in, (size_t) buf_inlen);
1.5 bcook 275: if (rv == 1) {
276: BIO_puts(out, "Signature Verified Successfully\n");
277: ret = 0;
278: } else
1.1 jsing 279: BIO_puts(out, "Signature Verification Failure\n");
280: if (rv >= 0)
281: goto end;
282: } else {
1.4 deraadt 283: rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
1.1 jsing 284: buf_in, (size_t) buf_inlen);
285: if (rv > 0) {
286: buf_out = malloc(buf_outlen);
287: if (!buf_out)
288: rv = -1;
289: else
290: rv = do_keyop(ctx, pkey_op,
291: buf_out, (size_t *) & buf_outlen,
292: buf_in, (size_t) buf_inlen);
293: }
294: }
295:
296: if (rv <= 0) {
297: BIO_printf(bio_err, "Public Key operation error\n");
298: ERR_print_errors(bio_err);
299: goto end;
300: }
301: ret = 0;
302: if (asn1parse) {
303: if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
304: ERR_print_errors(bio_err);
305: } else if (hexdump)
306: BIO_dump(out, (char *) buf_out, buf_outlen);
307: else
308: BIO_write(out, buf_out, buf_outlen);
309:
310: end:
311: if (ctx)
312: EVP_PKEY_CTX_free(ctx);
313: BIO_free(in);
314: BIO_free_all(out);
315: free(buf_in);
316: free(buf_out);
317: free(sig);
318:
319: return ret;
320: }
321:
322: static void
323: usage()
324: {
325: BIO_printf(bio_err, "Usage: pkeyutl [options]\n");
326: BIO_printf(bio_err, "-in file input file\n");
327: BIO_printf(bio_err, "-out file output file\n");
328: BIO_printf(bio_err, "-sigfile file signature file (verify operation only)\n");
329: BIO_printf(bio_err, "-inkey file input key\n");
330: BIO_printf(bio_err, "-keyform arg private key format - default PEM\n");
331: BIO_printf(bio_err, "-pubin input is a public key\n");
332: BIO_printf(bio_err, "-certin input is a certificate carrying a public key\n");
333: BIO_printf(bio_err, "-pkeyopt X:Y public key options\n");
334: BIO_printf(bio_err, "-sign sign with private key\n");
335: BIO_printf(bio_err, "-verify verify with public key\n");
336: BIO_printf(bio_err, "-verifyrecover verify with public key, recover original data\n");
337: BIO_printf(bio_err, "-encrypt encrypt with public key\n");
338: BIO_printf(bio_err, "-decrypt decrypt with private key\n");
339: BIO_printf(bio_err, "-derive derive shared secret\n");
340: BIO_printf(bio_err, "-hexdump hex dump output\n");
341: BIO_printf(bio_err, "-passin arg pass phrase source\n");
342:
343: }
344:
345: static EVP_PKEY_CTX *
346: init_ctx(int *pkeysize,
347: char *keyfile, int keyform, int key_type,
1.7 bcook 348: char *passargin, int pkey_op)
1.1 jsing 349: {
350: EVP_PKEY *pkey = NULL;
351: EVP_PKEY_CTX *ctx = NULL;
352: char *passin = NULL;
353: int rv = -1;
354: X509 *x;
355: if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
356: || (pkey_op == EVP_PKEY_OP_DERIVE))
357: && (key_type != KEY_PRIVKEY)) {
358: BIO_printf(bio_err, "A private key is needed for this operation\n");
359: goto end;
360: }
361: if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
362: BIO_printf(bio_err, "Error getting password\n");
363: goto end;
364: }
365: switch (key_type) {
366: case KEY_PRIVKEY:
367: pkey = load_key(bio_err, keyfile, keyform, 0,
1.7 bcook 368: passin, "Private Key");
1.1 jsing 369: break;
370:
371: case KEY_PUBKEY:
372: pkey = load_pubkey(bio_err, keyfile, keyform, 0,
1.7 bcook 373: NULL, "Public Key");
1.1 jsing 374: break;
375:
376: case KEY_CERT:
377: x = load_cert(bio_err, keyfile, keyform,
1.7 bcook 378: NULL, "Certificate");
1.1 jsing 379: if (x) {
380: pkey = X509_get_pubkey(x);
381: X509_free(x);
382: }
383: break;
384:
385: }
386:
387: *pkeysize = EVP_PKEY_size(pkey);
388:
389: if (!pkey)
390: goto end;
391:
1.7 bcook 392: ctx = EVP_PKEY_CTX_new(pkey, NULL);
1.1 jsing 393:
394: EVP_PKEY_free(pkey);
395:
396: if (!ctx)
397: goto end;
398:
399: switch (pkey_op) {
400: case EVP_PKEY_OP_SIGN:
401: rv = EVP_PKEY_sign_init(ctx);
402: break;
403:
404: case EVP_PKEY_OP_VERIFY:
405: rv = EVP_PKEY_verify_init(ctx);
406: break;
407:
408: case EVP_PKEY_OP_VERIFYRECOVER:
409: rv = EVP_PKEY_verify_recover_init(ctx);
410: break;
411:
412: case EVP_PKEY_OP_ENCRYPT:
413: rv = EVP_PKEY_encrypt_init(ctx);
414: break;
415:
416: case EVP_PKEY_OP_DECRYPT:
417: rv = EVP_PKEY_decrypt_init(ctx);
418: break;
419:
420: case EVP_PKEY_OP_DERIVE:
421: rv = EVP_PKEY_derive_init(ctx);
422: break;
423: }
424:
425: if (rv <= 0) {
426: EVP_PKEY_CTX_free(ctx);
427: ctx = NULL;
428: }
429: end:
430:
431: free(passin);
432:
433: return ctx;
434:
435:
436: }
437:
438: static int
439: setup_peer(BIO * err, EVP_PKEY_CTX * ctx, int peerform,
440: const char *file)
441: {
442: EVP_PKEY *peer = NULL;
443: int ret;
444: if (!ctx) {
445: BIO_puts(err, "-peerkey command before -inkey\n");
446: return 0;
447: }
1.7 bcook 448: peer = load_pubkey(bio_err, file, peerform, 0, NULL, "Peer Key");
1.1 jsing 449:
450: if (!peer) {
451: BIO_printf(bio_err, "Error reading peer key %s\n", file);
452: ERR_print_errors(err);
453: return 0;
454: }
455: ret = EVP_PKEY_derive_set_peer(ctx, peer);
456:
457: EVP_PKEY_free(peer);
458: if (ret <= 0)
459: ERR_print_errors(err);
460: return ret;
461: }
462:
463: static int
464: do_keyop(EVP_PKEY_CTX * ctx, int pkey_op,
465: unsigned char *out, size_t * poutlen,
466: unsigned char *in, size_t inlen)
467: {
468: int rv = 0;
469: switch (pkey_op) {
470: case EVP_PKEY_OP_VERIFYRECOVER:
471: rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
472: break;
473:
474: case EVP_PKEY_OP_SIGN:
475: rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
476: break;
477:
478: case EVP_PKEY_OP_ENCRYPT:
479: rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
480: break;
481:
482: case EVP_PKEY_OP_DECRYPT:
483: rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
484: break;
485:
486: case EVP_PKEY_OP_DERIVE:
487: rv = EVP_PKEY_derive(ctx, out, poutlen);
488: break;
489:
490: }
491: return rv;
492: }