]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
3 | * 2006. | |
9e4d0f0b DSH |
4 | */ |
5 | /* ==================================================================== | |
6 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
9e4d0f0b DSH |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | ||
9e4d0f0b DSH |
59 | #include "apps.h" |
60 | #include <string.h> | |
61 | #include <openssl/err.h> | |
62 | #include <openssl/pem.h> | |
63 | #include <openssl/evp.h> | |
64 | ||
0f113f3e MC |
65 | #define KEY_PRIVKEY 1 |
66 | #define KEY_PUBKEY 2 | |
67 | #define KEY_CERT 3 | |
9e4d0f0b | 68 | |
9e4d0f0b | 69 | static EVP_PKEY_CTX *init_ctx(int *pkeysize, |
0c20802c | 70 | const char *keyfile, int keyform, int key_type, |
9880236e M |
71 | char *passinarg, int pkey_op, ENGINE *e, |
72 | const int impl); | |
ffb1ac67 | 73 | |
0c20802c VD |
74 | static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file, |
75 | ENGINE *e); | |
9e4d0f0b | 76 | |
b010b7c4 | 77 | static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op, |
0f113f3e MC |
78 | unsigned char *out, size_t *poutlen, |
79 | unsigned char *in, size_t inlen); | |
b010b7c4 | 80 | |
7e1b7485 RS |
81 | typedef enum OPTION_choice { |
82 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
9880236e | 83 | OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT, |
7e1b7485 RS |
84 | OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN, |
85 | OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT, | |
86 | OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN, | |
87 | OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT | |
88 | } OPTION_CHOICE; | |
89 | ||
90 | OPTIONS pkeyutl_options[] = { | |
91 | {"help", OPT_HELP, '-', "Display this summary"}, | |
a173a7ee RS |
92 | {"in", OPT_IN, '<', "Input file - default stdin"}, |
93 | {"out", OPT_OUT, '>', "Output file - default stdout"}, | |
7e1b7485 RS |
94 | {"pubin", OPT_PUBIN, '-', "Input is a public key"}, |
95 | {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"}, | |
0c20802c | 96 | {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"}, |
7e1b7485 | 97 | {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"}, |
a173a7ee | 98 | {"sign", OPT_SIGN, '-', "Sign input data with private key"}, |
7e1b7485 RS |
99 | {"verify", OPT_VERIFY, '-', "Verify with public key"}, |
100 | {"verifyrecover", OPT_VERIFYRECOVER, '-', | |
101 | "Verify with public key, recover original data"}, | |
a173a7ee RS |
102 | {"rev", OPT_REV, '-', "Reverse the order of the input buffer"}, |
103 | {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"}, | |
104 | {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"}, | |
7e1b7485 RS |
105 | {"derive", OPT_DERIVE, '-', "Derive shared secret"}, |
106 | {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"}, | |
a173a7ee | 107 | {"inkey", OPT_INKEY, 's', "Input private key file"}, |
0c20802c | 108 | {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"}, |
7e1b7485 | 109 | {"passin", OPT_PASSIN, 's', "Pass phrase source"}, |
0c20802c VD |
110 | {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"}, |
111 | {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"}, | |
7e1b7485 RS |
112 | {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"}, |
113 | #ifndef OPENSSL_NO_ENGINE | |
114 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
a173a7ee RS |
115 | {"engine_impl", OPT_ENGINE_IMPL, '-', |
116 | "Also use engine given by -engine for crypto operations"}, | |
7e1b7485 RS |
117 | #endif |
118 | {NULL} | |
119 | }; | |
9e4d0f0b | 120 | |
7e1b7485 | 121 | int pkeyutl_main(int argc, char **argv) |
9e4d0f0b | 122 | { |
0f113f3e | 123 | BIO *in = NULL, *out = NULL; |
0f113f3e | 124 | ENGINE *e = NULL; |
0f113f3e | 125 | EVP_PKEY_CTX *ctx = NULL; |
7e1b7485 RS |
126 | char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL; |
127 | char hexdump = 0, asn1parse = 0, rev = 0, *prog; | |
0f113f3e | 128 | unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL; |
7e1b7485 RS |
129 | OPTION_CHOICE o; |
130 | int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = | |
131 | FORMAT_PEM; | |
132 | int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY; | |
9880236e | 133 | int engine_impl = 0; |
0f113f3e | 134 | int ret = 1, rv = -1; |
7e1b7485 | 135 | size_t buf_outlen; |
0c20802c VD |
136 | const char *inkey = NULL; |
137 | const char *peerkey = NULL; | |
138 | STACK_OF(OPENSSL_STRING) *pkeyopts = NULL; | |
0f113f3e | 139 | |
7e1b7485 RS |
140 | prog = opt_init(argc, argv, pkeyutl_options); |
141 | while ((o = opt_next()) != OPT_EOF) { | |
142 | switch (o) { | |
143 | case OPT_EOF: | |
144 | case OPT_ERR: | |
145 | opthelp: | |
146 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
147 | goto end; | |
148 | case OPT_HELP: | |
149 | opt_help(pkeyutl_options); | |
150 | ret = 0; | |
151 | goto end; | |
152 | case OPT_IN: | |
153 | infile = opt_arg(); | |
154 | break; | |
155 | case OPT_OUT: | |
156 | outfile = opt_arg(); | |
157 | break; | |
158 | case OPT_SIGFILE: | |
159 | sigfile = opt_arg(); | |
160 | break; | |
9880236e M |
161 | case OPT_ENGINE_IMPL: |
162 | engine_impl = 1; | |
163 | break; | |
7e1b7485 | 164 | case OPT_INKEY: |
0c20802c | 165 | inkey = opt_arg(); |
7e1b7485 RS |
166 | break; |
167 | case OPT_PEERKEY: | |
0c20802c | 168 | peerkey = opt_arg(); |
7e1b7485 RS |
169 | break; |
170 | case OPT_PASSIN: | |
171 | passinarg = opt_arg(); | |
172 | break; | |
173 | case OPT_PEERFORM: | |
0c20802c | 174 | if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform)) |
7e1b7485 RS |
175 | goto opthelp; |
176 | break; | |
177 | case OPT_KEYFORM: | |
0c20802c | 178 | if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform)) |
7e1b7485 RS |
179 | goto opthelp; |
180 | break; | |
7e1b7485 RS |
181 | case OPT_ENGINE: |
182 | e = setup_engine(opt_arg(), 0); | |
183 | break; | |
7e1b7485 | 184 | case OPT_PUBIN: |
0f113f3e | 185 | key_type = KEY_PUBKEY; |
7e1b7485 RS |
186 | break; |
187 | case OPT_CERTIN: | |
0f113f3e | 188 | key_type = KEY_CERT; |
7e1b7485 RS |
189 | break; |
190 | case OPT_ASN1PARSE: | |
0f113f3e | 191 | asn1parse = 1; |
7e1b7485 RS |
192 | break; |
193 | case OPT_HEXDUMP: | |
0f113f3e | 194 | hexdump = 1; |
7e1b7485 RS |
195 | break; |
196 | case OPT_SIGN: | |
0f113f3e | 197 | pkey_op = EVP_PKEY_OP_SIGN; |
7e1b7485 RS |
198 | break; |
199 | case OPT_VERIFY: | |
0f113f3e | 200 | pkey_op = EVP_PKEY_OP_VERIFY; |
7e1b7485 RS |
201 | break; |
202 | case OPT_VERIFYRECOVER: | |
0f113f3e | 203 | pkey_op = EVP_PKEY_OP_VERIFYRECOVER; |
7e1b7485 | 204 | break; |
7e1b7485 | 205 | case OPT_ENCRYPT: |
0f113f3e | 206 | pkey_op = EVP_PKEY_OP_ENCRYPT; |
7e1b7485 RS |
207 | break; |
208 | case OPT_DECRYPT: | |
0f113f3e | 209 | pkey_op = EVP_PKEY_OP_DECRYPT; |
7e1b7485 RS |
210 | break; |
211 | case OPT_DERIVE: | |
0f113f3e | 212 | pkey_op = EVP_PKEY_OP_DERIVE; |
7e1b7485 | 213 | break; |
0c20802c VD |
214 | case OPT_REV: |
215 | rev = 1; | |
216 | break; | |
7e1b7485 | 217 | case OPT_PKEYOPT: |
0c20802c VD |
218 | if ((pkeyopts == NULL && |
219 | (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) || | |
220 | sk_OPENSSL_STRING_push(pkeyopts, *++argv) == 0) { | |
221 | BIO_puts(bio_err, "out of memory\n"); | |
0f113f3e MC |
222 | goto end; |
223 | } | |
7e1b7485 | 224 | break; |
0f113f3e | 225 | } |
0f113f3e | 226 | } |
7e1b7485 RS |
227 | argc = opt_num_rest(); |
228 | argv = opt_rest(); | |
0f113f3e | 229 | |
0c20802c VD |
230 | if (inkey == NULL || |
231 | (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE)) | |
7e1b7485 | 232 | goto opthelp; |
0f113f3e | 233 | |
0c20802c | 234 | ctx = init_ctx(&keysize, inkey, keyform, key_type, |
9880236e | 235 | passinarg, pkey_op, e, engine_impl); |
0c20802c VD |
236 | if (ctx == NULL) { |
237 | BIO_printf(bio_err, "%s: Error initializing context\n", prog); | |
238 | ERR_print_errors(bio_err); | |
239 | goto end; | |
240 | } | |
241 | if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) { | |
242 | BIO_printf(bio_err, "%s: Error setting up peer key\n", prog); | |
243 | ERR_print_errors(bio_err); | |
244 | goto end; | |
245 | } | |
246 | if (pkeyopts != NULL) { | |
247 | int num = sk_OPENSSL_STRING_num(pkeyopts); | |
248 | int i; | |
249 | ||
250 | for (i = 0; i < num; ++i) { | |
251 | const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i); | |
252 | ||
253 | if (pkey_ctrl_string(ctx, opt) <= 0) { | |
254 | BIO_printf(bio_err, "%s: Can't set parameter:\n", prog); | |
255 | ERR_print_errors(bio_err); | |
256 | goto end; | |
257 | } | |
258 | } | |
259 | } | |
260 | ||
0f113f3e | 261 | if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) { |
7e1b7485 RS |
262 | BIO_printf(bio_err, |
263 | "%s: Signature file specified for non verify\n", prog); | |
0f113f3e MC |
264 | goto end; |
265 | } | |
266 | ||
267 | if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) { | |
7e1b7485 RS |
268 | BIO_printf(bio_err, |
269 | "%s: No signature file specified for verify\n", prog); | |
0f113f3e MC |
270 | goto end; |
271 | } | |
a9164153 | 272 | |
9e4d0f0b | 273 | /* FIXME: seed PRNG only if needed */ |
7e1b7485 | 274 | app_RAND_load_file(NULL, 0); |
0f113f3e MC |
275 | |
276 | if (pkey_op != EVP_PKEY_OP_DERIVE) { | |
bdd58d98 | 277 | in = bio_open_default(infile, 'r', FORMAT_BINARY); |
7e1b7485 | 278 | if (in == NULL) |
0f113f3e | 279 | goto end; |
0f113f3e | 280 | } |
bdd58d98 | 281 | out = bio_open_default(outfile, 'w', FORMAT_BINARY); |
7e1b7485 RS |
282 | if (out == NULL) |
283 | goto end; | |
0f113f3e MC |
284 | |
285 | if (sigfile) { | |
286 | BIO *sigbio = BIO_new_file(sigfile, "rb"); | |
287 | if (!sigbio) { | |
288 | BIO_printf(bio_err, "Can't open signature file %s\n", sigfile); | |
289 | goto end; | |
290 | } | |
291 | siglen = bio_to_mem(&sig, keysize * 10, sigbio); | |
292 | BIO_free(sigbio); | |
0c20802c | 293 | if (siglen < 0) { |
0f113f3e MC |
294 | BIO_printf(bio_err, "Error reading signature data\n"); |
295 | goto end; | |
296 | } | |
297 | } | |
298 | ||
299 | if (in) { | |
300 | /* Read the input data */ | |
301 | buf_inlen = bio_to_mem(&buf_in, keysize * 10, in); | |
0c20802c | 302 | if (buf_inlen < 0) { |
0f113f3e MC |
303 | BIO_printf(bio_err, "Error reading input Data\n"); |
304 | exit(1); | |
305 | } | |
306 | if (rev) { | |
307 | size_t i; | |
308 | unsigned char ctmp; | |
309 | size_t l = (size_t)buf_inlen; | |
310 | for (i = 0; i < l / 2; i++) { | |
311 | ctmp = buf_in[i]; | |
312 | buf_in[i] = buf_in[l - 1 - i]; | |
313 | buf_in[l - 1 - i] = ctmp; | |
314 | } | |
315 | } | |
316 | } | |
317 | ||
318 | if (pkey_op == EVP_PKEY_OP_VERIFY) { | |
319 | rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen, | |
320 | buf_in, (size_t)buf_inlen); | |
7e1b7485 | 321 | if (rv == 1) { |
0f113f3e | 322 | BIO_puts(out, "Signature Verified Successfully\n"); |
7e1b7485 RS |
323 | ret = 0; |
324 | } else | |
325 | BIO_puts(out, "Signature Verification Failure\n"); | |
326 | goto end; | |
327 | } | |
328 | rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen, | |
329 | buf_in, (size_t)buf_inlen); | |
0c20802c | 330 | if (rv > 0 && buf_outlen != 0) { |
68dc6824 RS |
331 | buf_out = app_malloc(buf_outlen, "buffer output"); |
332 | rv = do_keyop(ctx, pkey_op, | |
333 | buf_out, (size_t *)&buf_outlen, | |
334 | buf_in, (size_t)buf_inlen); | |
0f113f3e | 335 | } |
0c20802c | 336 | if (rv < 0) { |
0f113f3e MC |
337 | ERR_print_errors(bio_err); |
338 | goto end; | |
339 | } | |
340 | ret = 0; | |
7e1b7485 | 341 | |
0f113f3e MC |
342 | if (asn1parse) { |
343 | if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1)) | |
344 | ERR_print_errors(bio_err); | |
345 | } else if (hexdump) | |
346 | BIO_dump(out, (char *)buf_out, buf_outlen); | |
347 | else | |
348 | BIO_write(out, buf_out, buf_outlen); | |
349 | ||
350 | end: | |
c5ba2d99 | 351 | EVP_PKEY_CTX_free(ctx); |
0f113f3e MC |
352 | BIO_free(in); |
353 | BIO_free_all(out); | |
b548a1f1 RS |
354 | OPENSSL_free(buf_in); |
355 | OPENSSL_free(buf_out); | |
356 | OPENSSL_free(sig); | |
0c20802c | 357 | sk_OPENSSL_STRING_free(pkeyopts); |
0f113f3e | 358 | return ret; |
9e4d0f0b DSH |
359 | } |
360 | ||
9e4d0f0b | 361 | static EVP_PKEY_CTX *init_ctx(int *pkeysize, |
0c20802c | 362 | const char *keyfile, int keyform, int key_type, |
9880236e M |
363 | char *passinarg, int pkey_op, ENGINE *e, |
364 | const int engine_impl) | |
0f113f3e MC |
365 | { |
366 | EVP_PKEY *pkey = NULL; | |
367 | EVP_PKEY_CTX *ctx = NULL; | |
9880236e | 368 | ENGINE *impl = NULL; |
0f113f3e MC |
369 | char *passin = NULL; |
370 | int rv = -1; | |
371 | X509 *x; | |
372 | if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT) | |
373 | || (pkey_op == EVP_PKEY_OP_DERIVE)) | |
374 | && (key_type != KEY_PRIVKEY)) { | |
375 | BIO_printf(bio_err, "A private key is needed for this operation\n"); | |
376 | goto end; | |
377 | } | |
7e1b7485 | 378 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
379 | BIO_printf(bio_err, "Error getting password\n"); |
380 | goto end; | |
381 | } | |
382 | switch (key_type) { | |
383 | case KEY_PRIVKEY: | |
7e1b7485 | 384 | pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key"); |
0f113f3e MC |
385 | break; |
386 | ||
387 | case KEY_PUBKEY: | |
7e1b7485 | 388 | pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key"); |
0f113f3e MC |
389 | break; |
390 | ||
391 | case KEY_CERT: | |
7e1b7485 | 392 | x = load_cert(keyfile, keyform, NULL, e, "Certificate"); |
0f113f3e MC |
393 | if (x) { |
394 | pkey = X509_get_pubkey(x); | |
395 | X509_free(x); | |
396 | } | |
397 | break; | |
398 | ||
399 | } | |
400 | ||
401 | *pkeysize = EVP_PKEY_size(pkey); | |
402 | ||
403 | if (!pkey) | |
404 | goto end; | |
405 | ||
9880236e M |
406 | #ifndef OPENSSL_NO_ENGINE |
407 | if (engine_impl) | |
408 | impl = e; | |
409 | #endif | |
410 | ||
411 | ctx = EVP_PKEY_CTX_new(pkey, impl); | |
0f113f3e MC |
412 | |
413 | EVP_PKEY_free(pkey); | |
414 | ||
96487cdd | 415 | if (ctx == NULL) |
0f113f3e MC |
416 | goto end; |
417 | ||
418 | switch (pkey_op) { | |
419 | case EVP_PKEY_OP_SIGN: | |
420 | rv = EVP_PKEY_sign_init(ctx); | |
421 | break; | |
422 | ||
423 | case EVP_PKEY_OP_VERIFY: | |
424 | rv = EVP_PKEY_verify_init(ctx); | |
425 | break; | |
426 | ||
427 | case EVP_PKEY_OP_VERIFYRECOVER: | |
428 | rv = EVP_PKEY_verify_recover_init(ctx); | |
429 | break; | |
430 | ||
431 | case EVP_PKEY_OP_ENCRYPT: | |
432 | rv = EVP_PKEY_encrypt_init(ctx); | |
433 | break; | |
434 | ||
435 | case EVP_PKEY_OP_DECRYPT: | |
436 | rv = EVP_PKEY_decrypt_init(ctx); | |
437 | break; | |
438 | ||
439 | case EVP_PKEY_OP_DERIVE: | |
440 | rv = EVP_PKEY_derive_init(ctx); | |
441 | break; | |
442 | } | |
443 | ||
444 | if (rv <= 0) { | |
445 | EVP_PKEY_CTX_free(ctx); | |
446 | ctx = NULL; | |
447 | } | |
448 | ||
449 | end: | |
b548a1f1 | 450 | OPENSSL_free(passin); |
0f113f3e MC |
451 | return ctx; |
452 | ||
453 | } | |
9e4d0f0b | 454 | |
0c20802c VD |
455 | static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file, |
456 | ENGINE* e) | |
0f113f3e MC |
457 | { |
458 | EVP_PKEY *peer = NULL; | |
0c20802c | 459 | ENGINE* engine = NULL; |
0f113f3e | 460 | int ret; |
0f113f3e | 461 | |
0c20802c VD |
462 | if (peerform == FORMAT_ENGINE) |
463 | engine = e; | |
464 | peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key"); | |
0f113f3e MC |
465 | if (!peer) { |
466 | BIO_printf(bio_err, "Error reading peer key %s\n", file); | |
7e1b7485 | 467 | ERR_print_errors(bio_err); |
0f113f3e MC |
468 | return 0; |
469 | } | |
470 | ||
471 | ret = EVP_PKEY_derive_set_peer(ctx, peer); | |
472 | ||
473 | EVP_PKEY_free(peer); | |
474 | if (ret <= 0) | |
7e1b7485 | 475 | ERR_print_errors(bio_err); |
0f113f3e MC |
476 | return ret; |
477 | } | |
b010b7c4 DSH |
478 | |
479 | static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op, | |
0f113f3e MC |
480 | unsigned char *out, size_t *poutlen, |
481 | unsigned char *in, size_t inlen) | |
482 | { | |
483 | int rv = 0; | |
484 | switch (pkey_op) { | |
485 | case EVP_PKEY_OP_VERIFYRECOVER: | |
486 | rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen); | |
487 | break; | |
488 | ||
489 | case EVP_PKEY_OP_SIGN: | |
490 | rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen); | |
491 | break; | |
492 | ||
493 | case EVP_PKEY_OP_ENCRYPT: | |
494 | rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen); | |
495 | break; | |
496 | ||
497 | case EVP_PKEY_OP_DECRYPT: | |
498 | rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen); | |
499 | break; | |
500 | ||
501 | case EVP_PKEY_OP_DERIVE: | |
502 | rv = EVP_PKEY_derive(ctx, out, poutlen); | |
503 | break; | |
504 | ||
505 | } | |
506 | return rv; | |
507 | } |