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