apps: Don't include progs.h in apps.h
[openssl.git] / apps / pkeyutl.c
1 /*
2  * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "apps.h"
11 #include "progs.h"
12 #include <string.h>
13 #include <openssl/err.h>
14 #include <openssl/pem.h>
15 #include <openssl/evp.h>
16
17 #define KEY_NONE        0
18 #define KEY_PRIVKEY     1
19 #define KEY_PUBKEY      2
20 #define KEY_CERT        3
21
22 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
23                               const char *keyfile, int keyform, int key_type,
24                               char *passinarg, int pkey_op, ENGINE *e,
25                               const int impl);
26
27 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
28                       ENGINE *e);
29
30 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
31                     unsigned char *out, size_t *poutlen,
32                     const unsigned char *in, size_t inlen);
33
34 typedef enum OPTION_choice {
35     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
36     OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
37     OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
38     OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
39     OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
40     OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_KDF, OPT_KDFLEN,
41     OPT_R_ENUM
42 } OPTION_CHOICE;
43
44 const OPTIONS pkeyutl_options[] = {
45     {"help", OPT_HELP, '-', "Display this summary"},
46     {"in", OPT_IN, '<', "Input file - default stdin"},
47     {"out", OPT_OUT, '>', "Output file - default stdout"},
48     {"pubin", OPT_PUBIN, '-', "Input is a public key"},
49     {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
50     {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
51     {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
52     {"sign", OPT_SIGN, '-', "Sign input data with private key"},
53     {"verify", OPT_VERIFY, '-', "Verify with public key"},
54     {"verifyrecover", OPT_VERIFYRECOVER, '-',
55      "Verify with public key, recover original data"},
56     {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
57     {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
58     {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
59     {"derive", OPT_DERIVE, '-', "Derive shared secret"},
60     {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
61     {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
62     {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
63     {"inkey", OPT_INKEY, 's', "Input private key file"},
64     {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
65     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
66     {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
67     {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
68     {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
69     OPT_R_OPTIONS,
70 #ifndef OPENSSL_NO_ENGINE
71     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
72     {"engine_impl", OPT_ENGINE_IMPL, '-',
73      "Also use engine given by -engine for crypto operations"},
74 #endif
75     {NULL}
76 };
77
78 int pkeyutl_main(int argc, char **argv)
79 {
80     BIO *in = NULL, *out = NULL;
81     ENGINE *e = NULL;
82     EVP_PKEY_CTX *ctx = NULL;
83     char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
84     char hexdump = 0, asn1parse = 0, rev = 0, *prog;
85     unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
86     OPTION_CHOICE o;
87     int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform =
88         FORMAT_PEM;
89     int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
90     int engine_impl = 0;
91     int ret = 1, rv = -1;
92     size_t buf_outlen;
93     const char *inkey = NULL;
94     const char *peerkey = NULL;
95     const char *kdfalg = NULL;
96     int kdflen = 0;
97     STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
98
99     prog = opt_init(argc, argv, pkeyutl_options);
100     while ((o = opt_next()) != OPT_EOF) {
101         switch (o) {
102         case OPT_EOF:
103         case OPT_ERR:
104  opthelp:
105             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
106             goto end;
107         case OPT_HELP:
108             opt_help(pkeyutl_options);
109             ret = 0;
110             goto end;
111         case OPT_IN:
112             infile = opt_arg();
113             break;
114         case OPT_OUT:
115             outfile = opt_arg();
116             break;
117         case OPT_SIGFILE:
118             sigfile = opt_arg();
119             break;
120         case OPT_ENGINE_IMPL:
121             engine_impl = 1;
122             break;
123         case OPT_INKEY:
124             inkey = opt_arg();
125             break;
126         case OPT_PEERKEY:
127             peerkey = opt_arg();
128             break;
129         case OPT_PASSIN:
130             passinarg = opt_arg();
131             break;
132         case OPT_PEERFORM:
133             if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
134                 goto opthelp;
135             break;
136         case OPT_KEYFORM:
137             if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
138                 goto opthelp;
139             break;
140         case OPT_R_CASES:
141             if (!opt_rand(o))
142                 goto end;
143             break;
144         case OPT_ENGINE:
145             e = setup_engine(opt_arg(), 0);
146             break;
147         case OPT_PUBIN:
148             key_type = KEY_PUBKEY;
149             break;
150         case OPT_CERTIN:
151             key_type = KEY_CERT;
152             break;
153         case OPT_ASN1PARSE:
154             asn1parse = 1;
155             break;
156         case OPT_HEXDUMP:
157             hexdump = 1;
158             break;
159         case OPT_SIGN:
160             pkey_op = EVP_PKEY_OP_SIGN;
161             break;
162         case OPT_VERIFY:
163             pkey_op = EVP_PKEY_OP_VERIFY;
164             break;
165         case OPT_VERIFYRECOVER:
166             pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
167             break;
168         case OPT_ENCRYPT:
169             pkey_op = EVP_PKEY_OP_ENCRYPT;
170             break;
171         case OPT_DECRYPT:
172             pkey_op = EVP_PKEY_OP_DECRYPT;
173             break;
174         case OPT_DERIVE:
175             pkey_op = EVP_PKEY_OP_DERIVE;
176             break;
177         case OPT_KDF:
178             pkey_op = EVP_PKEY_OP_DERIVE;
179             key_type = KEY_NONE;
180             kdfalg = opt_arg();
181             break;
182         case OPT_KDFLEN:
183             kdflen = atoi(opt_arg());
184             break;
185         case OPT_REV:
186             rev = 1;
187             break;
188         case OPT_PKEYOPT:
189             if ((pkeyopts == NULL &&
190                  (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
191                 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
192                 BIO_puts(bio_err, "out of memory\n");
193                 goto end;
194             }
195             break;
196         }
197     }
198     argc = opt_num_rest();
199     if (argc != 0)
200         goto opthelp;
201
202     if (kdfalg != NULL) {
203         if (kdflen == 0)
204             goto opthelp;
205     } else if ((inkey == NULL)
206             || (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE)) {
207         goto opthelp;
208     }
209     ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
210                    passinarg, pkey_op, e, engine_impl);
211     if (ctx == NULL) {
212         BIO_printf(bio_err, "%s: Error initializing context\n", prog);
213         ERR_print_errors(bio_err);
214         goto end;
215     }
216     if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
217         BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
218         ERR_print_errors(bio_err);
219         goto end;
220     }
221     if (pkeyopts != NULL) {
222         int num = sk_OPENSSL_STRING_num(pkeyopts);
223         int i;
224
225         for (i = 0; i < num; ++i) {
226             const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
227
228             if (pkey_ctrl_string(ctx, opt) <= 0) {
229                 BIO_printf(bio_err, "%s: Can't set parameter:\n", prog);
230                 ERR_print_errors(bio_err);
231                 goto end;
232             }
233         }
234     }
235
236     if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
237         BIO_printf(bio_err,
238                    "%s: Signature file specified for non verify\n", prog);
239         goto end;
240     }
241
242     if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
243         BIO_printf(bio_err,
244                    "%s: No signature file specified for verify\n", prog);
245         goto end;
246     }
247
248     if (pkey_op != EVP_PKEY_OP_DERIVE) {
249         in = bio_open_default(infile, 'r', FORMAT_BINARY);
250         if (in == NULL)
251             goto end;
252     }
253     out = bio_open_default(outfile, 'w', FORMAT_BINARY);
254     if (out == NULL)
255         goto end;
256
257     if (sigfile != NULL) {
258         BIO *sigbio = BIO_new_file(sigfile, "rb");
259
260         if (sigbio == NULL) {
261             BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
262             goto end;
263         }
264         siglen = bio_to_mem(&sig, keysize * 10, sigbio);
265         BIO_free(sigbio);
266         if (siglen < 0) {
267             BIO_printf(bio_err, "Error reading signature data\n");
268             goto end;
269         }
270     }
271
272     if (in != NULL) {
273         /* Read the input data */
274         buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
275         if (buf_inlen < 0) {
276             BIO_printf(bio_err, "Error reading input Data\n");
277             exit(1);
278         }
279         if (rev) {
280             size_t i;
281             unsigned char ctmp;
282             size_t l = (size_t)buf_inlen;
283             for (i = 0; i < l / 2; i++) {
284                 ctmp = buf_in[i];
285                 buf_in[i] = buf_in[l - 1 - i];
286                 buf_in[l - 1 - i] = ctmp;
287             }
288         }
289     }
290
291     if (pkey_op == EVP_PKEY_OP_VERIFY) {
292         rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
293                              buf_in, (size_t)buf_inlen);
294         if (rv == 1) {
295             BIO_puts(out, "Signature Verified Successfully\n");
296             ret = 0;
297         } else {
298             BIO_puts(out, "Signature Verification Failure\n");
299         }
300         goto end;
301     }
302     if (kdflen != 0) {
303         buf_outlen = kdflen;
304         rv = 1;
305     } else {
306         rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
307                       buf_in, (size_t)buf_inlen);
308     }
309     if (rv > 0 && buf_outlen != 0) {
310         buf_out = app_malloc(buf_outlen, "buffer output");
311         rv = do_keyop(ctx, pkey_op,
312                       buf_out, (size_t *)&buf_outlen,
313                       buf_in, (size_t)buf_inlen);
314     }
315     if (rv <= 0) {
316         BIO_puts(bio_err, "Public Key operation error\n");
317         ERR_print_errors(bio_err);
318         goto end;
319     }
320     ret = 0;
321
322     if (asn1parse) {
323         if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
324             ERR_print_errors(bio_err);
325     } else if (hexdump) {
326         BIO_dump(out, (char *)buf_out, buf_outlen);
327     } else {
328         BIO_write(out, buf_out, buf_outlen);
329     }
330
331  end:
332     EVP_PKEY_CTX_free(ctx);
333     release_engine(e);
334     BIO_free(in);
335     BIO_free_all(out);
336     OPENSSL_free(buf_in);
337     OPENSSL_free(buf_out);
338     OPENSSL_free(sig);
339     sk_OPENSSL_STRING_free(pkeyopts);
340     return ret;
341 }
342
343 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
344                               const char *keyfile, int keyform, int key_type,
345                               char *passinarg, int pkey_op, ENGINE *e,
346                               const int engine_impl)
347 {
348     EVP_PKEY *pkey = NULL;
349     EVP_PKEY_CTX *ctx = NULL;
350     ENGINE *impl = NULL;
351     char *passin = NULL;
352     int rv = -1;
353     X509 *x;
354     if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
355          || (pkey_op == EVP_PKEY_OP_DERIVE))
356         && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
357         BIO_printf(bio_err, "A private key is needed for this operation\n");
358         goto end;
359     }
360     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
361         BIO_printf(bio_err, "Error getting password\n");
362         goto end;
363     }
364     switch (key_type) {
365     case KEY_PRIVKEY:
366         pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
367         break;
368
369     case KEY_PUBKEY:
370         pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
371         break;
372
373     case KEY_CERT:
374         x = load_cert(keyfile, keyform, "Certificate");
375         if (x) {
376             pkey = X509_get_pubkey(x);
377             X509_free(x);
378         }
379         break;
380
381     case KEY_NONE:
382         break;
383
384     }
385
386 #ifndef OPENSSL_NO_ENGINE
387     if (engine_impl)
388         impl = e;
389 #endif
390
391     if (kdfalg != NULL) {
392         int kdfnid = OBJ_sn2nid(kdfalg);
393
394         if (kdfnid == NID_undef) {
395             kdfnid = OBJ_ln2nid(kdfalg);
396             if (kdfnid == NID_undef)
397                 goto end;
398         }
399         ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
400     } else {
401         if (pkey == NULL)
402             goto end;
403         *pkeysize = EVP_PKEY_size(pkey);
404         ctx = EVP_PKEY_CTX_new(pkey, impl);
405         EVP_PKEY_free(pkey);
406     }
407
408     if (ctx == NULL)
409         goto end;
410
411     switch (pkey_op) {
412     case EVP_PKEY_OP_SIGN:
413         rv = EVP_PKEY_sign_init(ctx);
414         break;
415
416     case EVP_PKEY_OP_VERIFY:
417         rv = EVP_PKEY_verify_init(ctx);
418         break;
419
420     case EVP_PKEY_OP_VERIFYRECOVER:
421         rv = EVP_PKEY_verify_recover_init(ctx);
422         break;
423
424     case EVP_PKEY_OP_ENCRYPT:
425         rv = EVP_PKEY_encrypt_init(ctx);
426         break;
427
428     case EVP_PKEY_OP_DECRYPT:
429         rv = EVP_PKEY_decrypt_init(ctx);
430         break;
431
432     case EVP_PKEY_OP_DERIVE:
433         rv = EVP_PKEY_derive_init(ctx);
434         break;
435     }
436
437     if (rv <= 0) {
438         EVP_PKEY_CTX_free(ctx);
439         ctx = NULL;
440     }
441
442  end:
443     OPENSSL_free(passin);
444     return ctx;
445
446 }
447
448 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
449                       ENGINE* e)
450 {
451     EVP_PKEY *peer = NULL;
452     ENGINE* engine = NULL;
453     int ret;
454
455     if (peerform == FORMAT_ENGINE)
456         engine = e;
457     peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
458     if (peer == NULL) {
459         BIO_printf(bio_err, "Error reading peer key %s\n", file);
460         ERR_print_errors(bio_err);
461         return 0;
462     }
463
464     ret = EVP_PKEY_derive_set_peer(ctx, peer);
465
466     EVP_PKEY_free(peer);
467     if (ret <= 0)
468         ERR_print_errors(bio_err);
469     return ret;
470 }
471
472 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
473                     unsigned char *out, size_t *poutlen,
474                     const unsigned char *in, size_t inlen)
475 {
476     int rv = 0;
477     switch (pkey_op) {
478     case EVP_PKEY_OP_VERIFYRECOVER:
479         rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
480         break;
481
482     case EVP_PKEY_OP_SIGN:
483         rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
484         break;
485
486     case EVP_PKEY_OP_ENCRYPT:
487         rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
488         break;
489
490     case EVP_PKEY_OP_DECRYPT:
491         rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
492         break;
493
494     case EVP_PKEY_OP_DERIVE:
495         rv = EVP_PKEY_derive(ctx, out, poutlen);
496         break;
497
498     }
499     return rv;
500 }