1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "apps.h"
11e1051a39Sopenharmony_ci#include "progs.h"
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci#include <openssl/err.h>
14e1051a39Sopenharmony_ci#include <openssl/pem.h>
15e1051a39Sopenharmony_ci#include <openssl/evp.h>
16e1051a39Sopenharmony_ci#include <sys/stat.h>
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci#define KEY_NONE        0
19e1051a39Sopenharmony_ci#define KEY_PRIVKEY     1
20e1051a39Sopenharmony_ci#define KEY_PUBKEY      2
21e1051a39Sopenharmony_ci#define KEY_CERT        3
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_cistatic EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
24e1051a39Sopenharmony_ci                              const char *keyfile, int keyform, int key_type,
25e1051a39Sopenharmony_ci                              char *passinarg, int pkey_op, ENGINE *e,
26e1051a39Sopenharmony_ci                              const int impl, int rawin, EVP_PKEY **ppkey,
27e1051a39Sopenharmony_ci                              EVP_MD_CTX *mctx, const char *digestname,
28e1051a39Sopenharmony_ci                              OSSL_LIB_CTX *libctx, const char *propq);
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_cistatic int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
31e1051a39Sopenharmony_ci                      ENGINE *e);
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_cistatic int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
34e1051a39Sopenharmony_ci                    unsigned char *out, size_t *poutlen,
35e1051a39Sopenharmony_ci                    const unsigned char *in, size_t inlen);
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cistatic int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx,
38e1051a39Sopenharmony_ci                        EVP_PKEY *pkey, BIO *in,
39e1051a39Sopenharmony_ci                        int filesize, unsigned char *sig, int siglen,
40e1051a39Sopenharmony_ci                        unsigned char **out, size_t *poutlen);
41e1051a39Sopenharmony_ci
42e1051a39Sopenharmony_citypedef enum OPTION_choice {
43e1051a39Sopenharmony_ci    OPT_COMMON,
44e1051a39Sopenharmony_ci    OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
45e1051a39Sopenharmony_ci    OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
46e1051a39Sopenharmony_ci    OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
47e1051a39Sopenharmony_ci    OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
48e1051a39Sopenharmony_ci    OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_PKEYOPT_PASSIN, OPT_KDF,
49e1051a39Sopenharmony_ci    OPT_KDFLEN, OPT_R_ENUM, OPT_PROV_ENUM,
50e1051a39Sopenharmony_ci    OPT_CONFIG,
51e1051a39Sopenharmony_ci    OPT_RAWIN, OPT_DIGEST
52e1051a39Sopenharmony_ci} OPTION_CHOICE;
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ciconst OPTIONS pkeyutl_options[] = {
55e1051a39Sopenharmony_ci    OPT_SECTION("General"),
56e1051a39Sopenharmony_ci    {"help", OPT_HELP, '-', "Display this summary"},
57e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
58e1051a39Sopenharmony_ci    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
59e1051a39Sopenharmony_ci    {"engine_impl", OPT_ENGINE_IMPL, '-',
60e1051a39Sopenharmony_ci     "Also use engine given by -engine for crypto operations"},
61e1051a39Sopenharmony_ci#endif
62e1051a39Sopenharmony_ci    {"sign", OPT_SIGN, '-', "Sign input data with private key"},
63e1051a39Sopenharmony_ci    {"verify", OPT_VERIFY, '-', "Verify with public key"},
64e1051a39Sopenharmony_ci    {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
65e1051a39Sopenharmony_ci    {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
66e1051a39Sopenharmony_ci    {"derive", OPT_DERIVE, '-', "Derive shared secret"},
67e1051a39Sopenharmony_ci    OPT_CONFIG_OPTION,
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci    OPT_SECTION("Input"),
70e1051a39Sopenharmony_ci    {"in", OPT_IN, '<', "Input file - default stdin"},
71e1051a39Sopenharmony_ci    {"rawin", OPT_RAWIN, '-', "Indicate the input data is in raw form"},
72e1051a39Sopenharmony_ci    {"pubin", OPT_PUBIN, '-', "Input is a public key"},
73e1051a39Sopenharmony_ci    {"inkey", OPT_INKEY, 's', "Input private key file"},
74e1051a39Sopenharmony_ci    {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
75e1051a39Sopenharmony_ci    {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
76e1051a39Sopenharmony_ci    {"peerform", OPT_PEERFORM, 'E', "Peer key format (DER/PEM/P12/ENGINE)"},
77e1051a39Sopenharmony_ci    {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
78e1051a39Sopenharmony_ci    {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
79e1051a39Sopenharmony_ci    {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
80e1051a39Sopenharmony_ci    {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"},
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci    OPT_SECTION("Output"),
83e1051a39Sopenharmony_ci    {"out", OPT_OUT, '>', "Output file - default stdout"},
84e1051a39Sopenharmony_ci    {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
85e1051a39Sopenharmony_ci    {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
86e1051a39Sopenharmony_ci    {"verifyrecover", OPT_VERIFYRECOVER, '-',
87e1051a39Sopenharmony_ci     "Verify with public key, recover original data"},
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    OPT_SECTION("Signing/Derivation"),
90e1051a39Sopenharmony_ci    {"digest", OPT_DIGEST, 's',
91e1051a39Sopenharmony_ci     "Specify the digest algorithm when signing the raw input data"},
92e1051a39Sopenharmony_ci    {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
93e1051a39Sopenharmony_ci    {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's',
94e1051a39Sopenharmony_ci     "Public key option that is read as a passphrase argument opt:passphrase"},
95e1051a39Sopenharmony_ci    {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
96e1051a39Sopenharmony_ci    {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci    OPT_R_OPTIONS,
99e1051a39Sopenharmony_ci    OPT_PROV_OPTIONS,
100e1051a39Sopenharmony_ci    {NULL}
101e1051a39Sopenharmony_ci};
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ciint pkeyutl_main(int argc, char **argv)
104e1051a39Sopenharmony_ci{
105e1051a39Sopenharmony_ci    CONF *conf = NULL;
106e1051a39Sopenharmony_ci    BIO *in = NULL, *out = NULL;
107e1051a39Sopenharmony_ci    ENGINE *e = NULL;
108e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
109e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
110e1051a39Sopenharmony_ci    char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
111e1051a39Sopenharmony_ci    char hexdump = 0, asn1parse = 0, rev = 0, *prog;
112e1051a39Sopenharmony_ci    unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
113e1051a39Sopenharmony_ci    OPTION_CHOICE o;
114e1051a39Sopenharmony_ci    int buf_inlen = 0, siglen = -1;
115e1051a39Sopenharmony_ci    int keyform = FORMAT_UNDEF, peerform = FORMAT_UNDEF;
116e1051a39Sopenharmony_ci    int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
117e1051a39Sopenharmony_ci    int engine_impl = 0;
118e1051a39Sopenharmony_ci    int ret = 1, rv = -1;
119e1051a39Sopenharmony_ci    size_t buf_outlen;
120e1051a39Sopenharmony_ci    const char *inkey = NULL;
121e1051a39Sopenharmony_ci    const char *peerkey = NULL;
122e1051a39Sopenharmony_ci    const char *kdfalg = NULL, *digestname = NULL;
123e1051a39Sopenharmony_ci    int kdflen = 0;
124e1051a39Sopenharmony_ci    STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
125e1051a39Sopenharmony_ci    STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
126e1051a39Sopenharmony_ci    int rawin = 0;
127e1051a39Sopenharmony_ci    EVP_MD_CTX *mctx = NULL;
128e1051a39Sopenharmony_ci    EVP_MD *md = NULL;
129e1051a39Sopenharmony_ci    int filesize = -1;
130e1051a39Sopenharmony_ci    OSSL_LIB_CTX *libctx = app_get0_libctx();
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_ci    prog = opt_init(argc, argv, pkeyutl_options);
133e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
134e1051a39Sopenharmony_ci        switch (o) {
135e1051a39Sopenharmony_ci        case OPT_EOF:
136e1051a39Sopenharmony_ci        case OPT_ERR:
137e1051a39Sopenharmony_ci opthelp:
138e1051a39Sopenharmony_ci            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
139e1051a39Sopenharmony_ci            goto end;
140e1051a39Sopenharmony_ci        case OPT_HELP:
141e1051a39Sopenharmony_ci            opt_help(pkeyutl_options);
142e1051a39Sopenharmony_ci            ret = 0;
143e1051a39Sopenharmony_ci            goto end;
144e1051a39Sopenharmony_ci        case OPT_IN:
145e1051a39Sopenharmony_ci            infile = opt_arg();
146e1051a39Sopenharmony_ci            break;
147e1051a39Sopenharmony_ci        case OPT_OUT:
148e1051a39Sopenharmony_ci            outfile = opt_arg();
149e1051a39Sopenharmony_ci            break;
150e1051a39Sopenharmony_ci        case OPT_SIGFILE:
151e1051a39Sopenharmony_ci            sigfile = opt_arg();
152e1051a39Sopenharmony_ci            break;
153e1051a39Sopenharmony_ci        case OPT_ENGINE_IMPL:
154e1051a39Sopenharmony_ci            engine_impl = 1;
155e1051a39Sopenharmony_ci            break;
156e1051a39Sopenharmony_ci        case OPT_INKEY:
157e1051a39Sopenharmony_ci            inkey = opt_arg();
158e1051a39Sopenharmony_ci            break;
159e1051a39Sopenharmony_ci        case OPT_PEERKEY:
160e1051a39Sopenharmony_ci            peerkey = opt_arg();
161e1051a39Sopenharmony_ci            break;
162e1051a39Sopenharmony_ci        case OPT_PASSIN:
163e1051a39Sopenharmony_ci            passinarg = opt_arg();
164e1051a39Sopenharmony_ci            break;
165e1051a39Sopenharmony_ci        case OPT_PEERFORM:
166e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_ANY, &peerform))
167e1051a39Sopenharmony_ci                goto opthelp;
168e1051a39Sopenharmony_ci            break;
169e1051a39Sopenharmony_ci        case OPT_KEYFORM:
170e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
171e1051a39Sopenharmony_ci                goto opthelp;
172e1051a39Sopenharmony_ci            break;
173e1051a39Sopenharmony_ci        case OPT_R_CASES:
174e1051a39Sopenharmony_ci            if (!opt_rand(o))
175e1051a39Sopenharmony_ci                goto end;
176e1051a39Sopenharmony_ci            break;
177e1051a39Sopenharmony_ci        case OPT_CONFIG:
178e1051a39Sopenharmony_ci            conf = app_load_config_modules(opt_arg());
179e1051a39Sopenharmony_ci            if (conf == NULL)
180e1051a39Sopenharmony_ci                goto end;
181e1051a39Sopenharmony_ci            break;
182e1051a39Sopenharmony_ci        case OPT_PROV_CASES:
183e1051a39Sopenharmony_ci            if (!opt_provider(o))
184e1051a39Sopenharmony_ci                goto end;
185e1051a39Sopenharmony_ci            break;
186e1051a39Sopenharmony_ci        case OPT_ENGINE:
187e1051a39Sopenharmony_ci            e = setup_engine(opt_arg(), 0);
188e1051a39Sopenharmony_ci            break;
189e1051a39Sopenharmony_ci        case OPT_PUBIN:
190e1051a39Sopenharmony_ci            key_type = KEY_PUBKEY;
191e1051a39Sopenharmony_ci            break;
192e1051a39Sopenharmony_ci        case OPT_CERTIN:
193e1051a39Sopenharmony_ci            key_type = KEY_CERT;
194e1051a39Sopenharmony_ci            break;
195e1051a39Sopenharmony_ci        case OPT_ASN1PARSE:
196e1051a39Sopenharmony_ci            asn1parse = 1;
197e1051a39Sopenharmony_ci            break;
198e1051a39Sopenharmony_ci        case OPT_HEXDUMP:
199e1051a39Sopenharmony_ci            hexdump = 1;
200e1051a39Sopenharmony_ci            break;
201e1051a39Sopenharmony_ci        case OPT_SIGN:
202e1051a39Sopenharmony_ci            pkey_op = EVP_PKEY_OP_SIGN;
203e1051a39Sopenharmony_ci            break;
204e1051a39Sopenharmony_ci        case OPT_VERIFY:
205e1051a39Sopenharmony_ci            pkey_op = EVP_PKEY_OP_VERIFY;
206e1051a39Sopenharmony_ci            break;
207e1051a39Sopenharmony_ci        case OPT_VERIFYRECOVER:
208e1051a39Sopenharmony_ci            pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
209e1051a39Sopenharmony_ci            break;
210e1051a39Sopenharmony_ci        case OPT_ENCRYPT:
211e1051a39Sopenharmony_ci            pkey_op = EVP_PKEY_OP_ENCRYPT;
212e1051a39Sopenharmony_ci            break;
213e1051a39Sopenharmony_ci        case OPT_DECRYPT:
214e1051a39Sopenharmony_ci            pkey_op = EVP_PKEY_OP_DECRYPT;
215e1051a39Sopenharmony_ci            break;
216e1051a39Sopenharmony_ci        case OPT_DERIVE:
217e1051a39Sopenharmony_ci            pkey_op = EVP_PKEY_OP_DERIVE;
218e1051a39Sopenharmony_ci            break;
219e1051a39Sopenharmony_ci        case OPT_KDF:
220e1051a39Sopenharmony_ci            pkey_op = EVP_PKEY_OP_DERIVE;
221e1051a39Sopenharmony_ci            key_type = KEY_NONE;
222e1051a39Sopenharmony_ci            kdfalg = opt_arg();
223e1051a39Sopenharmony_ci            break;
224e1051a39Sopenharmony_ci        case OPT_KDFLEN:
225e1051a39Sopenharmony_ci            kdflen = atoi(opt_arg());
226e1051a39Sopenharmony_ci            break;
227e1051a39Sopenharmony_ci        case OPT_REV:
228e1051a39Sopenharmony_ci            rev = 1;
229e1051a39Sopenharmony_ci            break;
230e1051a39Sopenharmony_ci        case OPT_PKEYOPT:
231e1051a39Sopenharmony_ci            if ((pkeyopts == NULL &&
232e1051a39Sopenharmony_ci                 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
233e1051a39Sopenharmony_ci                sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
234e1051a39Sopenharmony_ci                BIO_puts(bio_err, "out of memory\n");
235e1051a39Sopenharmony_ci                goto end;
236e1051a39Sopenharmony_ci            }
237e1051a39Sopenharmony_ci            break;
238e1051a39Sopenharmony_ci        case OPT_PKEYOPT_PASSIN:
239e1051a39Sopenharmony_ci            if ((pkeyopts_passin == NULL &&
240e1051a39Sopenharmony_ci                 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
241e1051a39Sopenharmony_ci                sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
242e1051a39Sopenharmony_ci                BIO_puts(bio_err, "out of memory\n");
243e1051a39Sopenharmony_ci                goto end;
244e1051a39Sopenharmony_ci            }
245e1051a39Sopenharmony_ci            break;
246e1051a39Sopenharmony_ci        case OPT_RAWIN:
247e1051a39Sopenharmony_ci            rawin = 1;
248e1051a39Sopenharmony_ci            break;
249e1051a39Sopenharmony_ci        case OPT_DIGEST:
250e1051a39Sopenharmony_ci            digestname = opt_arg();
251e1051a39Sopenharmony_ci            break;
252e1051a39Sopenharmony_ci        }
253e1051a39Sopenharmony_ci    }
254e1051a39Sopenharmony_ci
255e1051a39Sopenharmony_ci    /* No extra arguments. */
256e1051a39Sopenharmony_ci    argc = opt_num_rest();
257e1051a39Sopenharmony_ci    if (argc != 0)
258e1051a39Sopenharmony_ci        goto opthelp;
259e1051a39Sopenharmony_ci
260e1051a39Sopenharmony_ci    if (!app_RAND_load())
261e1051a39Sopenharmony_ci        goto end;
262e1051a39Sopenharmony_ci
263e1051a39Sopenharmony_ci    if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
264e1051a39Sopenharmony_ci        BIO_printf(bio_err,
265e1051a39Sopenharmony_ci                   "%s: -rawin can only be used with -sign or -verify\n",
266e1051a39Sopenharmony_ci                   prog);
267e1051a39Sopenharmony_ci        goto opthelp;
268e1051a39Sopenharmony_ci    }
269e1051a39Sopenharmony_ci
270e1051a39Sopenharmony_ci    if (digestname != NULL && !rawin) {
271e1051a39Sopenharmony_ci        BIO_printf(bio_err,
272e1051a39Sopenharmony_ci                   "%s: -digest can only be used with -rawin\n",
273e1051a39Sopenharmony_ci                   prog);
274e1051a39Sopenharmony_ci        goto opthelp;
275e1051a39Sopenharmony_ci    }
276e1051a39Sopenharmony_ci
277e1051a39Sopenharmony_ci    if (rawin && rev) {
278e1051a39Sopenharmony_ci        BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
279e1051a39Sopenharmony_ci                   prog);
280e1051a39Sopenharmony_ci        goto opthelp;
281e1051a39Sopenharmony_ci    }
282e1051a39Sopenharmony_ci
283e1051a39Sopenharmony_ci    if (kdfalg != NULL) {
284e1051a39Sopenharmony_ci        if (kdflen == 0) {
285e1051a39Sopenharmony_ci            BIO_printf(bio_err,
286e1051a39Sopenharmony_ci                       "%s: no KDF length given (-kdflen parameter).\n", prog);
287e1051a39Sopenharmony_ci            goto opthelp;
288e1051a39Sopenharmony_ci        }
289e1051a39Sopenharmony_ci    } else if (inkey == NULL) {
290e1051a39Sopenharmony_ci        BIO_printf(bio_err,
291e1051a39Sopenharmony_ci                   "%s: no private key given (-inkey parameter).\n", prog);
292e1051a39Sopenharmony_ci        goto opthelp;
293e1051a39Sopenharmony_ci    } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
294e1051a39Sopenharmony_ci        BIO_printf(bio_err,
295e1051a39Sopenharmony_ci                   "%s: no peer key given (-peerkey parameter).\n", prog);
296e1051a39Sopenharmony_ci        goto opthelp;
297e1051a39Sopenharmony_ci    }
298e1051a39Sopenharmony_ci
299e1051a39Sopenharmony_ci    if (rawin) {
300e1051a39Sopenharmony_ci        if ((mctx = EVP_MD_CTX_new()) == NULL) {
301e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Error: out of memory\n");
302e1051a39Sopenharmony_ci            goto end;
303e1051a39Sopenharmony_ci        }
304e1051a39Sopenharmony_ci    }
305e1051a39Sopenharmony_ci    ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
306e1051a39Sopenharmony_ci                   passinarg, pkey_op, e, engine_impl, rawin, &pkey,
307e1051a39Sopenharmony_ci                   mctx, digestname, libctx, app_get0_propq());
308e1051a39Sopenharmony_ci    if (ctx == NULL) {
309e1051a39Sopenharmony_ci        BIO_printf(bio_err, "%s: Error initializing context\n", prog);
310e1051a39Sopenharmony_ci        goto end;
311e1051a39Sopenharmony_ci    }
312e1051a39Sopenharmony_ci    if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
313e1051a39Sopenharmony_ci        BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
314e1051a39Sopenharmony_ci        goto end;
315e1051a39Sopenharmony_ci    }
316e1051a39Sopenharmony_ci    if (pkeyopts != NULL) {
317e1051a39Sopenharmony_ci        int num = sk_OPENSSL_STRING_num(pkeyopts);
318e1051a39Sopenharmony_ci        int i;
319e1051a39Sopenharmony_ci
320e1051a39Sopenharmony_ci        for (i = 0; i < num; ++i) {
321e1051a39Sopenharmony_ci            const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
322e1051a39Sopenharmony_ci
323e1051a39Sopenharmony_ci            if (pkey_ctrl_string(ctx, opt) <= 0) {
324e1051a39Sopenharmony_ci                BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
325e1051a39Sopenharmony_ci                           prog, opt);
326e1051a39Sopenharmony_ci                goto end;
327e1051a39Sopenharmony_ci            }
328e1051a39Sopenharmony_ci        }
329e1051a39Sopenharmony_ci    }
330e1051a39Sopenharmony_ci    if (pkeyopts_passin != NULL) {
331e1051a39Sopenharmony_ci        int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
332e1051a39Sopenharmony_ci        int i;
333e1051a39Sopenharmony_ci
334e1051a39Sopenharmony_ci        for (i = 0; i < num; i++) {
335e1051a39Sopenharmony_ci            char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
336e1051a39Sopenharmony_ci            char *passin = strchr(opt, ':');
337e1051a39Sopenharmony_ci            char *passwd;
338e1051a39Sopenharmony_ci
339e1051a39Sopenharmony_ci            if (passin == NULL) {
340e1051a39Sopenharmony_ci                /* Get password interactively */
341e1051a39Sopenharmony_ci                char passwd_buf[4096];
342e1051a39Sopenharmony_ci                int r;
343e1051a39Sopenharmony_ci
344e1051a39Sopenharmony_ci                BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
345e1051a39Sopenharmony_ci                r = EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
346e1051a39Sopenharmony_ci                                       passwd_buf, 0);
347e1051a39Sopenharmony_ci                if (r < 0) {
348e1051a39Sopenharmony_ci                    if (r == -2)
349e1051a39Sopenharmony_ci                        BIO_puts(bio_err, "user abort\n");
350e1051a39Sopenharmony_ci                    else
351e1051a39Sopenharmony_ci                        BIO_puts(bio_err, "entry failed\n");
352e1051a39Sopenharmony_ci                    goto end;
353e1051a39Sopenharmony_ci                }
354e1051a39Sopenharmony_ci                passwd = OPENSSL_strdup(passwd_buf);
355e1051a39Sopenharmony_ci                if (passwd == NULL) {
356e1051a39Sopenharmony_ci                    BIO_puts(bio_err, "out of memory\n");
357e1051a39Sopenharmony_ci                    goto end;
358e1051a39Sopenharmony_ci                }
359e1051a39Sopenharmony_ci            } else {
360e1051a39Sopenharmony_ci                /* Get password as a passin argument: First split option name
361e1051a39Sopenharmony_ci                 * and passphrase argument into two strings */
362e1051a39Sopenharmony_ci                *passin = 0;
363e1051a39Sopenharmony_ci                passin++;
364e1051a39Sopenharmony_ci                if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
365e1051a39Sopenharmony_ci                    BIO_printf(bio_err, "failed to get '%s'\n", opt);
366e1051a39Sopenharmony_ci                    goto end;
367e1051a39Sopenharmony_ci                }
368e1051a39Sopenharmony_ci            }
369e1051a39Sopenharmony_ci
370e1051a39Sopenharmony_ci            if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
371e1051a39Sopenharmony_ci                BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
372e1051a39Sopenharmony_ci                           prog, opt);
373e1051a39Sopenharmony_ci                goto end;
374e1051a39Sopenharmony_ci            }
375e1051a39Sopenharmony_ci            OPENSSL_free(passwd);
376e1051a39Sopenharmony_ci        }
377e1051a39Sopenharmony_ci    }
378e1051a39Sopenharmony_ci
379e1051a39Sopenharmony_ci    if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
380e1051a39Sopenharmony_ci        BIO_printf(bio_err,
381e1051a39Sopenharmony_ci                   "%s: Signature file specified for non verify\n", prog);
382e1051a39Sopenharmony_ci        goto end;
383e1051a39Sopenharmony_ci    }
384e1051a39Sopenharmony_ci
385e1051a39Sopenharmony_ci    if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
386e1051a39Sopenharmony_ci        BIO_printf(bio_err,
387e1051a39Sopenharmony_ci                   "%s: No signature file specified for verify\n", prog);
388e1051a39Sopenharmony_ci        goto end;
389e1051a39Sopenharmony_ci    }
390e1051a39Sopenharmony_ci
391e1051a39Sopenharmony_ci    if (pkey_op != EVP_PKEY_OP_DERIVE) {
392e1051a39Sopenharmony_ci        in = bio_open_default(infile, 'r', FORMAT_BINARY);
393e1051a39Sopenharmony_ci        if (infile != NULL) {
394e1051a39Sopenharmony_ci            struct stat st;
395e1051a39Sopenharmony_ci
396e1051a39Sopenharmony_ci            if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
397e1051a39Sopenharmony_ci                filesize = (int)st.st_size;
398e1051a39Sopenharmony_ci        }
399e1051a39Sopenharmony_ci        if (in == NULL)
400e1051a39Sopenharmony_ci            goto end;
401e1051a39Sopenharmony_ci    }
402e1051a39Sopenharmony_ci    out = bio_open_default(outfile, 'w', FORMAT_BINARY);
403e1051a39Sopenharmony_ci    if (out == NULL)
404e1051a39Sopenharmony_ci        goto end;
405e1051a39Sopenharmony_ci
406e1051a39Sopenharmony_ci    if (sigfile != NULL) {
407e1051a39Sopenharmony_ci        BIO *sigbio = BIO_new_file(sigfile, "rb");
408e1051a39Sopenharmony_ci
409e1051a39Sopenharmony_ci        if (sigbio == NULL) {
410e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
411e1051a39Sopenharmony_ci            goto end;
412e1051a39Sopenharmony_ci        }
413e1051a39Sopenharmony_ci        siglen = bio_to_mem(&sig, keysize * 10, sigbio);
414e1051a39Sopenharmony_ci        BIO_free(sigbio);
415e1051a39Sopenharmony_ci        if (siglen < 0) {
416e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Error reading signature data\n");
417e1051a39Sopenharmony_ci            goto end;
418e1051a39Sopenharmony_ci        }
419e1051a39Sopenharmony_ci    }
420e1051a39Sopenharmony_ci
421e1051a39Sopenharmony_ci    /* Raw input data is handled elsewhere */
422e1051a39Sopenharmony_ci    if (in != NULL && !rawin) {
423e1051a39Sopenharmony_ci        /* Read the input data */
424e1051a39Sopenharmony_ci        buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
425e1051a39Sopenharmony_ci        if (buf_inlen < 0) {
426e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Error reading input Data\n");
427e1051a39Sopenharmony_ci            goto end;
428e1051a39Sopenharmony_ci        }
429e1051a39Sopenharmony_ci        if (rev) {
430e1051a39Sopenharmony_ci            size_t i;
431e1051a39Sopenharmony_ci            unsigned char ctmp;
432e1051a39Sopenharmony_ci            size_t l = (size_t)buf_inlen;
433e1051a39Sopenharmony_ci            for (i = 0; i < l / 2; i++) {
434e1051a39Sopenharmony_ci                ctmp = buf_in[i];
435e1051a39Sopenharmony_ci                buf_in[i] = buf_in[l - 1 - i];
436e1051a39Sopenharmony_ci                buf_in[l - 1 - i] = ctmp;
437e1051a39Sopenharmony_ci            }
438e1051a39Sopenharmony_ci        }
439e1051a39Sopenharmony_ci    }
440e1051a39Sopenharmony_ci
441e1051a39Sopenharmony_ci    /* Sanity check the input if the input is not raw */
442e1051a39Sopenharmony_ci    if (!rawin
443e1051a39Sopenharmony_ci            && buf_inlen > EVP_MAX_MD_SIZE
444e1051a39Sopenharmony_ci            && (pkey_op == EVP_PKEY_OP_SIGN
445e1051a39Sopenharmony_ci                || pkey_op == EVP_PKEY_OP_VERIFY)) {
446e1051a39Sopenharmony_ci        BIO_printf(bio_err,
447e1051a39Sopenharmony_ci                   "Error: The input data looks too long to be a hash\n");
448e1051a39Sopenharmony_ci        goto end;
449e1051a39Sopenharmony_ci    }
450e1051a39Sopenharmony_ci
451e1051a39Sopenharmony_ci    if (pkey_op == EVP_PKEY_OP_VERIFY) {
452e1051a39Sopenharmony_ci        if (rawin) {
453e1051a39Sopenharmony_ci            rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, sig, siglen,
454e1051a39Sopenharmony_ci                              NULL, 0);
455e1051a39Sopenharmony_ci        } else {
456e1051a39Sopenharmony_ci            rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
457e1051a39Sopenharmony_ci                                 buf_in, (size_t)buf_inlen);
458e1051a39Sopenharmony_ci        }
459e1051a39Sopenharmony_ci        if (rv == 1) {
460e1051a39Sopenharmony_ci            BIO_puts(out, "Signature Verified Successfully\n");
461e1051a39Sopenharmony_ci            ret = 0;
462e1051a39Sopenharmony_ci        } else {
463e1051a39Sopenharmony_ci            BIO_puts(out, "Signature Verification Failure\n");
464e1051a39Sopenharmony_ci        }
465e1051a39Sopenharmony_ci        goto end;
466e1051a39Sopenharmony_ci    }
467e1051a39Sopenharmony_ci    if (rawin) {
468e1051a39Sopenharmony_ci        /* rawin allocates the buffer in do_raw_keyop() */
469e1051a39Sopenharmony_ci        rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, NULL, 0,
470e1051a39Sopenharmony_ci                          &buf_out, (size_t *)&buf_outlen);
471e1051a39Sopenharmony_ci    } else {
472e1051a39Sopenharmony_ci        if (kdflen != 0) {
473e1051a39Sopenharmony_ci            buf_outlen = kdflen;
474e1051a39Sopenharmony_ci            rv = 1;
475e1051a39Sopenharmony_ci        } else {
476e1051a39Sopenharmony_ci            rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
477e1051a39Sopenharmony_ci                          buf_in, (size_t)buf_inlen);
478e1051a39Sopenharmony_ci        }
479e1051a39Sopenharmony_ci        if (rv > 0 && buf_outlen != 0) {
480e1051a39Sopenharmony_ci            buf_out = app_malloc(buf_outlen, "buffer output");
481e1051a39Sopenharmony_ci            rv = do_keyop(ctx, pkey_op,
482e1051a39Sopenharmony_ci                          buf_out, (size_t *)&buf_outlen,
483e1051a39Sopenharmony_ci                          buf_in, (size_t)buf_inlen);
484e1051a39Sopenharmony_ci        }
485e1051a39Sopenharmony_ci    }
486e1051a39Sopenharmony_ci    if (rv <= 0) {
487e1051a39Sopenharmony_ci        if (pkey_op != EVP_PKEY_OP_DERIVE) {
488e1051a39Sopenharmony_ci            BIO_puts(bio_err, "Public Key operation error\n");
489e1051a39Sopenharmony_ci        } else {
490e1051a39Sopenharmony_ci            BIO_puts(bio_err, "Key derivation failed\n");
491e1051a39Sopenharmony_ci        }
492e1051a39Sopenharmony_ci        goto end;
493e1051a39Sopenharmony_ci    }
494e1051a39Sopenharmony_ci    ret = 0;
495e1051a39Sopenharmony_ci
496e1051a39Sopenharmony_ci    if (asn1parse) {
497e1051a39Sopenharmony_ci        if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
498e1051a39Sopenharmony_ci            ERR_print_errors(bio_err); /* but still return success */
499e1051a39Sopenharmony_ci    } else if (hexdump) {
500e1051a39Sopenharmony_ci        BIO_dump(out, (char *)buf_out, buf_outlen);
501e1051a39Sopenharmony_ci    } else {
502e1051a39Sopenharmony_ci        BIO_write(out, buf_out, buf_outlen);
503e1051a39Sopenharmony_ci    }
504e1051a39Sopenharmony_ci
505e1051a39Sopenharmony_ci end:
506e1051a39Sopenharmony_ci    if (ret != 0)
507e1051a39Sopenharmony_ci        ERR_print_errors(bio_err);
508e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mctx);
509e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
510e1051a39Sopenharmony_ci    EVP_MD_free(md);
511e1051a39Sopenharmony_ci    release_engine(e);
512e1051a39Sopenharmony_ci    BIO_free(in);
513e1051a39Sopenharmony_ci    BIO_free_all(out);
514e1051a39Sopenharmony_ci    OPENSSL_free(buf_in);
515e1051a39Sopenharmony_ci    OPENSSL_free(buf_out);
516e1051a39Sopenharmony_ci    OPENSSL_free(sig);
517e1051a39Sopenharmony_ci    sk_OPENSSL_STRING_free(pkeyopts);
518e1051a39Sopenharmony_ci    sk_OPENSSL_STRING_free(pkeyopts_passin);
519e1051a39Sopenharmony_ci    NCONF_free(conf);
520e1051a39Sopenharmony_ci    return ret;
521e1051a39Sopenharmony_ci}
522e1051a39Sopenharmony_ci
523e1051a39Sopenharmony_cistatic EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
524e1051a39Sopenharmony_ci                              const char *keyfile, int keyform, int key_type,
525e1051a39Sopenharmony_ci                              char *passinarg, int pkey_op, ENGINE *e,
526e1051a39Sopenharmony_ci                              const int engine_impl, int rawin,
527e1051a39Sopenharmony_ci                              EVP_PKEY **ppkey, EVP_MD_CTX *mctx, const char *digestname,
528e1051a39Sopenharmony_ci                              OSSL_LIB_CTX *libctx, const char *propq)
529e1051a39Sopenharmony_ci{
530e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
531e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
532e1051a39Sopenharmony_ci    ENGINE *impl = NULL;
533e1051a39Sopenharmony_ci    char *passin = NULL;
534e1051a39Sopenharmony_ci    int rv = -1;
535e1051a39Sopenharmony_ci    X509 *x;
536e1051a39Sopenharmony_ci
537e1051a39Sopenharmony_ci    if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
538e1051a39Sopenharmony_ci         || (pkey_op == EVP_PKEY_OP_DERIVE))
539e1051a39Sopenharmony_ci        && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
540e1051a39Sopenharmony_ci        BIO_printf(bio_err, "A private key is needed for this operation\n");
541e1051a39Sopenharmony_ci        goto end;
542e1051a39Sopenharmony_ci    }
543e1051a39Sopenharmony_ci    if (!app_passwd(passinarg, NULL, &passin, NULL)) {
544e1051a39Sopenharmony_ci        BIO_printf(bio_err, "Error getting password\n");
545e1051a39Sopenharmony_ci        goto end;
546e1051a39Sopenharmony_ci    }
547e1051a39Sopenharmony_ci    switch (key_type) {
548e1051a39Sopenharmony_ci    case KEY_PRIVKEY:
549e1051a39Sopenharmony_ci        pkey = load_key(keyfile, keyform, 0, passin, e, "private key");
550e1051a39Sopenharmony_ci        break;
551e1051a39Sopenharmony_ci
552e1051a39Sopenharmony_ci    case KEY_PUBKEY:
553e1051a39Sopenharmony_ci        pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key");
554e1051a39Sopenharmony_ci        break;
555e1051a39Sopenharmony_ci
556e1051a39Sopenharmony_ci    case KEY_CERT:
557e1051a39Sopenharmony_ci        x = load_cert(keyfile, keyform, "Certificate");
558e1051a39Sopenharmony_ci        if (x) {
559e1051a39Sopenharmony_ci            pkey = X509_get_pubkey(x);
560e1051a39Sopenharmony_ci            X509_free(x);
561e1051a39Sopenharmony_ci        }
562e1051a39Sopenharmony_ci        break;
563e1051a39Sopenharmony_ci
564e1051a39Sopenharmony_ci    case KEY_NONE:
565e1051a39Sopenharmony_ci        break;
566e1051a39Sopenharmony_ci
567e1051a39Sopenharmony_ci    }
568e1051a39Sopenharmony_ci
569e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
570e1051a39Sopenharmony_ci    if (engine_impl)
571e1051a39Sopenharmony_ci        impl = e;
572e1051a39Sopenharmony_ci#endif
573e1051a39Sopenharmony_ci
574e1051a39Sopenharmony_ci    if (kdfalg != NULL) {
575e1051a39Sopenharmony_ci        int kdfnid = OBJ_sn2nid(kdfalg);
576e1051a39Sopenharmony_ci
577e1051a39Sopenharmony_ci        if (kdfnid == NID_undef) {
578e1051a39Sopenharmony_ci            kdfnid = OBJ_ln2nid(kdfalg);
579e1051a39Sopenharmony_ci            if (kdfnid == NID_undef) {
580e1051a39Sopenharmony_ci                BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
581e1051a39Sopenharmony_ci                           kdfalg);
582e1051a39Sopenharmony_ci                goto end;
583e1051a39Sopenharmony_ci            }
584e1051a39Sopenharmony_ci        }
585e1051a39Sopenharmony_ci        if (impl != NULL)
586e1051a39Sopenharmony_ci            ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
587e1051a39Sopenharmony_ci        else
588e1051a39Sopenharmony_ci            ctx = EVP_PKEY_CTX_new_from_name(libctx, kdfalg, propq);
589e1051a39Sopenharmony_ci    } else {
590e1051a39Sopenharmony_ci        if (pkey == NULL)
591e1051a39Sopenharmony_ci            goto end;
592e1051a39Sopenharmony_ci
593e1051a39Sopenharmony_ci        *pkeysize = EVP_PKEY_get_size(pkey);
594e1051a39Sopenharmony_ci        if (impl != NULL)
595e1051a39Sopenharmony_ci            ctx = EVP_PKEY_CTX_new(pkey, impl);
596e1051a39Sopenharmony_ci        else
597e1051a39Sopenharmony_ci            ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
598e1051a39Sopenharmony_ci        if (ppkey != NULL)
599e1051a39Sopenharmony_ci            *ppkey = pkey;
600e1051a39Sopenharmony_ci        EVP_PKEY_free(pkey);
601e1051a39Sopenharmony_ci    }
602e1051a39Sopenharmony_ci
603e1051a39Sopenharmony_ci    if (ctx == NULL)
604e1051a39Sopenharmony_ci        goto end;
605e1051a39Sopenharmony_ci
606e1051a39Sopenharmony_ci    if (rawin) {
607e1051a39Sopenharmony_ci        EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
608e1051a39Sopenharmony_ci
609e1051a39Sopenharmony_ci        switch (pkey_op) {
610e1051a39Sopenharmony_ci        case EVP_PKEY_OP_SIGN:
611e1051a39Sopenharmony_ci            rv = EVP_DigestSignInit_ex(mctx, NULL, digestname, libctx, propq,
612e1051a39Sopenharmony_ci                                       pkey, NULL);
613e1051a39Sopenharmony_ci            break;
614e1051a39Sopenharmony_ci
615e1051a39Sopenharmony_ci        case EVP_PKEY_OP_VERIFY:
616e1051a39Sopenharmony_ci            rv = EVP_DigestVerifyInit_ex(mctx, NULL, digestname, libctx, propq,
617e1051a39Sopenharmony_ci                                         pkey, NULL);
618e1051a39Sopenharmony_ci            break;
619e1051a39Sopenharmony_ci        }
620e1051a39Sopenharmony_ci
621e1051a39Sopenharmony_ci    } else {
622e1051a39Sopenharmony_ci        switch (pkey_op) {
623e1051a39Sopenharmony_ci        case EVP_PKEY_OP_SIGN:
624e1051a39Sopenharmony_ci            rv = EVP_PKEY_sign_init(ctx);
625e1051a39Sopenharmony_ci            break;
626e1051a39Sopenharmony_ci
627e1051a39Sopenharmony_ci        case EVP_PKEY_OP_VERIFY:
628e1051a39Sopenharmony_ci            rv = EVP_PKEY_verify_init(ctx);
629e1051a39Sopenharmony_ci            break;
630e1051a39Sopenharmony_ci
631e1051a39Sopenharmony_ci        case EVP_PKEY_OP_VERIFYRECOVER:
632e1051a39Sopenharmony_ci            rv = EVP_PKEY_verify_recover_init(ctx);
633e1051a39Sopenharmony_ci            break;
634e1051a39Sopenharmony_ci
635e1051a39Sopenharmony_ci        case EVP_PKEY_OP_ENCRYPT:
636e1051a39Sopenharmony_ci            rv = EVP_PKEY_encrypt_init(ctx);
637e1051a39Sopenharmony_ci            break;
638e1051a39Sopenharmony_ci
639e1051a39Sopenharmony_ci        case EVP_PKEY_OP_DECRYPT:
640e1051a39Sopenharmony_ci            rv = EVP_PKEY_decrypt_init(ctx);
641e1051a39Sopenharmony_ci            break;
642e1051a39Sopenharmony_ci
643e1051a39Sopenharmony_ci        case EVP_PKEY_OP_DERIVE:
644e1051a39Sopenharmony_ci            rv = EVP_PKEY_derive_init(ctx);
645e1051a39Sopenharmony_ci            break;
646e1051a39Sopenharmony_ci        }
647e1051a39Sopenharmony_ci    }
648e1051a39Sopenharmony_ci
649e1051a39Sopenharmony_ci    if (rv <= 0) {
650e1051a39Sopenharmony_ci        EVP_PKEY_CTX_free(ctx);
651e1051a39Sopenharmony_ci        ctx = NULL;
652e1051a39Sopenharmony_ci    }
653e1051a39Sopenharmony_ci
654e1051a39Sopenharmony_ci end:
655e1051a39Sopenharmony_ci    OPENSSL_free(passin);
656e1051a39Sopenharmony_ci    return ctx;
657e1051a39Sopenharmony_ci
658e1051a39Sopenharmony_ci}
659e1051a39Sopenharmony_ci
660e1051a39Sopenharmony_cistatic int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
661e1051a39Sopenharmony_ci                      ENGINE *e)
662e1051a39Sopenharmony_ci{
663e1051a39Sopenharmony_ci    EVP_PKEY *peer = NULL;
664e1051a39Sopenharmony_ci    ENGINE *engine = NULL;
665e1051a39Sopenharmony_ci    int ret;
666e1051a39Sopenharmony_ci
667e1051a39Sopenharmony_ci    if (peerform == FORMAT_ENGINE)
668e1051a39Sopenharmony_ci        engine = e;
669e1051a39Sopenharmony_ci    peer = load_pubkey(file, peerform, 0, NULL, engine, "peer key");
670e1051a39Sopenharmony_ci    if (peer == NULL) {
671e1051a39Sopenharmony_ci        BIO_printf(bio_err, "Error reading peer key %s\n", file);
672e1051a39Sopenharmony_ci        return 0;
673e1051a39Sopenharmony_ci    }
674e1051a39Sopenharmony_ci
675e1051a39Sopenharmony_ci    ret = EVP_PKEY_derive_set_peer(ctx, peer) > 0;
676e1051a39Sopenharmony_ci
677e1051a39Sopenharmony_ci    EVP_PKEY_free(peer);
678e1051a39Sopenharmony_ci    return ret;
679e1051a39Sopenharmony_ci}
680e1051a39Sopenharmony_ci
681e1051a39Sopenharmony_cistatic int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
682e1051a39Sopenharmony_ci                    unsigned char *out, size_t *poutlen,
683e1051a39Sopenharmony_ci                    const unsigned char *in, size_t inlen)
684e1051a39Sopenharmony_ci{
685e1051a39Sopenharmony_ci    int rv = 0;
686e1051a39Sopenharmony_ci    switch (pkey_op) {
687e1051a39Sopenharmony_ci    case EVP_PKEY_OP_VERIFYRECOVER:
688e1051a39Sopenharmony_ci        rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
689e1051a39Sopenharmony_ci        break;
690e1051a39Sopenharmony_ci
691e1051a39Sopenharmony_ci    case EVP_PKEY_OP_SIGN:
692e1051a39Sopenharmony_ci        rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
693e1051a39Sopenharmony_ci        break;
694e1051a39Sopenharmony_ci
695e1051a39Sopenharmony_ci    case EVP_PKEY_OP_ENCRYPT:
696e1051a39Sopenharmony_ci        rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
697e1051a39Sopenharmony_ci        break;
698e1051a39Sopenharmony_ci
699e1051a39Sopenharmony_ci    case EVP_PKEY_OP_DECRYPT:
700e1051a39Sopenharmony_ci        rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
701e1051a39Sopenharmony_ci        break;
702e1051a39Sopenharmony_ci
703e1051a39Sopenharmony_ci    case EVP_PKEY_OP_DERIVE:
704e1051a39Sopenharmony_ci        rv = EVP_PKEY_derive(ctx, out, poutlen);
705e1051a39Sopenharmony_ci        break;
706e1051a39Sopenharmony_ci
707e1051a39Sopenharmony_ci    }
708e1051a39Sopenharmony_ci    return rv;
709e1051a39Sopenharmony_ci}
710e1051a39Sopenharmony_ci
711e1051a39Sopenharmony_ci#define TBUF_MAXSIZE 2048
712e1051a39Sopenharmony_ci
713e1051a39Sopenharmony_cistatic int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx,
714e1051a39Sopenharmony_ci                        EVP_PKEY *pkey, BIO *in,
715e1051a39Sopenharmony_ci                        int filesize, unsigned char *sig, int siglen,
716e1051a39Sopenharmony_ci                        unsigned char **out, size_t *poutlen)
717e1051a39Sopenharmony_ci{
718e1051a39Sopenharmony_ci    int rv = 0;
719e1051a39Sopenharmony_ci    unsigned char tbuf[TBUF_MAXSIZE];
720e1051a39Sopenharmony_ci    unsigned char *mbuf = NULL;
721e1051a39Sopenharmony_ci    int buf_len = 0;
722e1051a39Sopenharmony_ci
723e1051a39Sopenharmony_ci    /* Some algorithms only support oneshot digests */
724e1051a39Sopenharmony_ci    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED25519
725e1051a39Sopenharmony_ci            || EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448) {
726e1051a39Sopenharmony_ci        if (filesize < 0) {
727e1051a39Sopenharmony_ci            BIO_printf(bio_err,
728e1051a39Sopenharmony_ci                       "Error: unable to determine file size for oneshot operation\n");
729e1051a39Sopenharmony_ci            goto end;
730e1051a39Sopenharmony_ci        }
731e1051a39Sopenharmony_ci        mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
732e1051a39Sopenharmony_ci        switch(pkey_op) {
733e1051a39Sopenharmony_ci        case EVP_PKEY_OP_VERIFY:
734e1051a39Sopenharmony_ci            buf_len = BIO_read(in, mbuf, filesize);
735e1051a39Sopenharmony_ci            if (buf_len != filesize) {
736e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error reading raw input data\n");
737e1051a39Sopenharmony_ci                goto end;
738e1051a39Sopenharmony_ci            }
739e1051a39Sopenharmony_ci            rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
740e1051a39Sopenharmony_ci            break;
741e1051a39Sopenharmony_ci        case EVP_PKEY_OP_SIGN:
742e1051a39Sopenharmony_ci            buf_len = BIO_read(in, mbuf, filesize);
743e1051a39Sopenharmony_ci            if (buf_len != filesize) {
744e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error reading raw input data\n");
745e1051a39Sopenharmony_ci                goto end;
746e1051a39Sopenharmony_ci            }
747e1051a39Sopenharmony_ci            rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
748e1051a39Sopenharmony_ci            if (rv == 1 && out != NULL) {
749e1051a39Sopenharmony_ci                *out = app_malloc(*poutlen, "buffer output");
750e1051a39Sopenharmony_ci                rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
751e1051a39Sopenharmony_ci            }
752e1051a39Sopenharmony_ci            break;
753e1051a39Sopenharmony_ci        }
754e1051a39Sopenharmony_ci        goto end;
755e1051a39Sopenharmony_ci    }
756e1051a39Sopenharmony_ci
757e1051a39Sopenharmony_ci    switch(pkey_op) {
758e1051a39Sopenharmony_ci    case EVP_PKEY_OP_VERIFY:
759e1051a39Sopenharmony_ci        for (;;) {
760e1051a39Sopenharmony_ci            buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
761e1051a39Sopenharmony_ci            if (buf_len == 0)
762e1051a39Sopenharmony_ci                break;
763e1051a39Sopenharmony_ci            if (buf_len < 0) {
764e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error reading raw input data\n");
765e1051a39Sopenharmony_ci                goto end;
766e1051a39Sopenharmony_ci            }
767e1051a39Sopenharmony_ci            rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len);
768e1051a39Sopenharmony_ci            if (rv != 1) {
769e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error verifying raw input data\n");
770e1051a39Sopenharmony_ci                goto end;
771e1051a39Sopenharmony_ci            }
772e1051a39Sopenharmony_ci        }
773e1051a39Sopenharmony_ci        rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
774e1051a39Sopenharmony_ci        break;
775e1051a39Sopenharmony_ci    case EVP_PKEY_OP_SIGN:
776e1051a39Sopenharmony_ci        for (;;) {
777e1051a39Sopenharmony_ci            buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
778e1051a39Sopenharmony_ci            if (buf_len == 0)
779e1051a39Sopenharmony_ci                break;
780e1051a39Sopenharmony_ci            if (buf_len < 0) {
781e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error reading raw input data\n");
782e1051a39Sopenharmony_ci                goto end;
783e1051a39Sopenharmony_ci            }
784e1051a39Sopenharmony_ci            rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
785e1051a39Sopenharmony_ci            if (rv != 1) {
786e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error signing raw input data\n");
787e1051a39Sopenharmony_ci                goto end;
788e1051a39Sopenharmony_ci            }
789e1051a39Sopenharmony_ci        }
790e1051a39Sopenharmony_ci        rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
791e1051a39Sopenharmony_ci        if (rv == 1 && out != NULL) {
792e1051a39Sopenharmony_ci            *out = app_malloc(*poutlen, "buffer output");
793e1051a39Sopenharmony_ci            rv = EVP_DigestSignFinal(mctx, *out, poutlen);
794e1051a39Sopenharmony_ci        }
795e1051a39Sopenharmony_ci        break;
796e1051a39Sopenharmony_ci    }
797e1051a39Sopenharmony_ci
798e1051a39Sopenharmony_ci end:
799e1051a39Sopenharmony_ci    OPENSSL_free(mbuf);
800e1051a39Sopenharmony_ci    return rv;
801e1051a39Sopenharmony_ci}
802