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