VirtualBox

source: vbox/trunk/src/libs/openssl-3.4.1/apps/pkeyutl.c@ 109302

Last change on this file since 109302 was 109052, checked in by vboxsync, 4 weeks ago

openssl-3.4.1: Applied our changes, regenerated files, added missing files and functions. This time with a three way merge. ​bugref:10890

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette