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