]>
Commit | Line | Data |
---|---|---|
1 | /* pmeth_lib.c */ | |
2 | /* | |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
4 | * 2006. | |
5 | */ | |
6 | /* ==================================================================== | |
7 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * | |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
60 | #include <stdio.h> | |
61 | #include <stdlib.h> | |
62 | #include "internal/cryptlib.h" | |
63 | #ifndef OPENSSL_NO_ENGINE | |
64 | # include <openssl/engine.h> | |
65 | #endif | |
66 | #include "internal/asn1_int.h" | |
67 | #include "internal/evp_int.h" | |
68 | ||
69 | typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); | |
70 | ||
71 | DECLARE_STACK_OF(EVP_PKEY_METHOD) | |
72 | static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; | |
73 | ||
74 | static const EVP_PKEY_METHOD *standard_methods[] = { | |
75 | #ifndef OPENSSL_NO_RSA | |
76 | &rsa_pkey_meth, | |
77 | #endif | |
78 | #ifndef OPENSSL_NO_DH | |
79 | &dh_pkey_meth, | |
80 | #endif | |
81 | #ifndef OPENSSL_NO_DSA | |
82 | &dsa_pkey_meth, | |
83 | #endif | |
84 | #ifndef OPENSSL_NO_EC | |
85 | &ec_pkey_meth, | |
86 | #endif | |
87 | &hmac_pkey_meth, | |
88 | &cmac_pkey_meth, | |
89 | #ifndef OPENSSL_NO_DH | |
90 | &dhx_pkey_meth | |
91 | #endif | |
92 | }; | |
93 | ||
94 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, | |
95 | pmeth); | |
96 | ||
97 | static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, | |
98 | const EVP_PKEY_METHOD *const *b) | |
99 | { | |
100 | return ((*a)->pkey_id - (*b)->pkey_id); | |
101 | } | |
102 | ||
103 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, | |
104 | pmeth); | |
105 | ||
106 | const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) | |
107 | { | |
108 | EVP_PKEY_METHOD tmp; | |
109 | const EVP_PKEY_METHOD *t = &tmp, **ret; | |
110 | tmp.pkey_id = type; | |
111 | if (app_pkey_methods) { | |
112 | int idx; | |
113 | idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); | |
114 | if (idx >= 0) | |
115 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
116 | } | |
117 | ret = OBJ_bsearch_pmeth(&t, standard_methods, | |
118 | sizeof(standard_methods) / | |
119 | sizeof(EVP_PKEY_METHOD *)); | |
120 | if (!ret || !*ret) | |
121 | return NULL; | |
122 | return *ret; | |
123 | } | |
124 | ||
125 | static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) | |
126 | { | |
127 | EVP_PKEY_CTX *ret; | |
128 | const EVP_PKEY_METHOD *pmeth; | |
129 | if (id == -1) { | |
130 | if (!pkey || !pkey->ameth) | |
131 | return NULL; | |
132 | id = pkey->ameth->pkey_id; | |
133 | } | |
134 | #ifndef OPENSSL_NO_ENGINE | |
135 | if (pkey && pkey->engine) | |
136 | e = pkey->engine; | |
137 | /* Try to find an ENGINE which implements this method */ | |
138 | if (e) { | |
139 | if (!ENGINE_init(e)) { | |
140 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB); | |
141 | return NULL; | |
142 | } | |
143 | } else | |
144 | e = ENGINE_get_pkey_meth_engine(id); | |
145 | ||
146 | /* | |
147 | * If an ENGINE handled this method look it up. Othewise use internal | |
148 | * tables. | |
149 | */ | |
150 | ||
151 | if (e) | |
152 | pmeth = ENGINE_get_pkey_meth(e, id); | |
153 | else | |
154 | #endif | |
155 | pmeth = EVP_PKEY_meth_find(id); | |
156 | ||
157 | if (pmeth == NULL) { | |
158 | EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM); | |
159 | return NULL; | |
160 | } | |
161 | ||
162 | ret = OPENSSL_zalloc(sizeof(*ret)); | |
163 | if (!ret) { | |
164 | #ifndef OPENSSL_NO_ENGINE | |
165 | if (e) | |
166 | ENGINE_finish(e); | |
167 | #endif | |
168 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
169 | return NULL; | |
170 | } | |
171 | ret->engine = e; | |
172 | ret->pmeth = pmeth; | |
173 | ret->operation = EVP_PKEY_OP_UNDEFINED; | |
174 | ret->pkey = pkey; | |
175 | if (pkey) | |
176 | CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | |
177 | ||
178 | if (pmeth->init) { | |
179 | if (pmeth->init(ret) <= 0) { | |
180 | EVP_PKEY_CTX_free(ret); | |
181 | return NULL; | |
182 | } | |
183 | } | |
184 | ||
185 | return ret; | |
186 | } | |
187 | ||
188 | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) | |
189 | { | |
190 | EVP_PKEY_METHOD *pmeth; | |
191 | ||
192 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); | |
193 | if (!pmeth) | |
194 | return NULL; | |
195 | ||
196 | pmeth->pkey_id = id; | |
197 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | |
198 | return pmeth; | |
199 | } | |
200 | ||
201 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, | |
202 | const EVP_PKEY_METHOD *meth) | |
203 | { | |
204 | if (ppkey_id) | |
205 | *ppkey_id = meth->pkey_id; | |
206 | if (pflags) | |
207 | *pflags = meth->flags; | |
208 | } | |
209 | ||
210 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | |
211 | { | |
212 | ||
213 | dst->init = src->init; | |
214 | dst->copy = src->copy; | |
215 | dst->cleanup = src->cleanup; | |
216 | ||
217 | dst->paramgen_init = src->paramgen_init; | |
218 | dst->paramgen = src->paramgen; | |
219 | ||
220 | dst->keygen_init = src->keygen_init; | |
221 | dst->keygen = src->keygen; | |
222 | ||
223 | dst->sign_init = src->sign_init; | |
224 | dst->sign = src->sign; | |
225 | ||
226 | dst->verify_init = src->verify_init; | |
227 | dst->verify = src->verify; | |
228 | ||
229 | dst->verify_recover_init = src->verify_recover_init; | |
230 | dst->verify_recover = src->verify_recover; | |
231 | ||
232 | dst->signctx_init = src->signctx_init; | |
233 | dst->signctx = src->signctx; | |
234 | ||
235 | dst->verifyctx_init = src->verifyctx_init; | |
236 | dst->verifyctx = src->verifyctx; | |
237 | ||
238 | dst->encrypt_init = src->encrypt_init; | |
239 | dst->encrypt = src->encrypt; | |
240 | ||
241 | dst->decrypt_init = src->decrypt_init; | |
242 | dst->decrypt = src->decrypt; | |
243 | ||
244 | dst->derive_init = src->derive_init; | |
245 | dst->derive = src->derive; | |
246 | ||
247 | dst->ctrl = src->ctrl; | |
248 | dst->ctrl_str = src->ctrl_str; | |
249 | } | |
250 | ||
251 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) | |
252 | { | |
253 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | |
254 | OPENSSL_free(pmeth); | |
255 | } | |
256 | ||
257 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) | |
258 | { | |
259 | return int_ctx_new(pkey, e, -1); | |
260 | } | |
261 | ||
262 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) | |
263 | { | |
264 | return int_ctx_new(NULL, e, id); | |
265 | } | |
266 | ||
267 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx) | |
268 | { | |
269 | EVP_PKEY_CTX *rctx; | |
270 | if (!pctx->pmeth || !pctx->pmeth->copy) | |
271 | return NULL; | |
272 | #ifndef OPENSSL_NO_ENGINE | |
273 | /* Make sure it's safe to copy a pkey context using an ENGINE */ | |
274 | if (pctx->engine && !ENGINE_init(pctx->engine)) { | |
275 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); | |
276 | return 0; | |
277 | } | |
278 | #endif | |
279 | rctx = OPENSSL_malloc(sizeof(*rctx)); | |
280 | if (!rctx) | |
281 | return NULL; | |
282 | ||
283 | rctx->pmeth = pctx->pmeth; | |
284 | #ifndef OPENSSL_NO_ENGINE | |
285 | rctx->engine = pctx->engine; | |
286 | #endif | |
287 | ||
288 | if (pctx->pkey) | |
289 | CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | |
290 | ||
291 | rctx->pkey = pctx->pkey; | |
292 | ||
293 | if (pctx->peerkey) | |
294 | CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | |
295 | ||
296 | rctx->peerkey = pctx->peerkey; | |
297 | ||
298 | rctx->data = NULL; | |
299 | rctx->app_data = NULL; | |
300 | rctx->operation = pctx->operation; | |
301 | ||
302 | if (pctx->pmeth->copy(rctx, pctx) > 0) | |
303 | return rctx; | |
304 | ||
305 | EVP_PKEY_CTX_free(rctx); | |
306 | return NULL; | |
307 | ||
308 | } | |
309 | ||
310 | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) | |
311 | { | |
312 | if (app_pkey_methods == NULL) { | |
313 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); | |
314 | if (!app_pkey_methods) | |
315 | return 0; | |
316 | } | |
317 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) | |
318 | return 0; | |
319 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); | |
320 | return 1; | |
321 | } | |
322 | ||
323 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) | |
324 | { | |
325 | if (ctx == NULL) | |
326 | return; | |
327 | if (ctx->pmeth && ctx->pmeth->cleanup) | |
328 | ctx->pmeth->cleanup(ctx); | |
329 | EVP_PKEY_free(ctx->pkey); | |
330 | EVP_PKEY_free(ctx->peerkey); | |
331 | #ifndef OPENSSL_NO_ENGINE | |
332 | if (ctx->engine) | |
333 | /* | |
334 | * The EVP_PKEY_CTX we used belongs to an ENGINE, release the | |
335 | * functional reference we held for this reason. | |
336 | */ | |
337 | ENGINE_finish(ctx->engine); | |
338 | #endif | |
339 | OPENSSL_free(ctx); | |
340 | } | |
341 | ||
342 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, | |
343 | int cmd, int p1, void *p2) | |
344 | { | |
345 | int ret; | |
346 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) { | |
347 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
348 | return -2; | |
349 | } | |
350 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) | |
351 | return -1; | |
352 | ||
353 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { | |
354 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); | |
355 | return -1; | |
356 | } | |
357 | ||
358 | if ((optype != -1) && !(ctx->operation & optype)) { | |
359 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); | |
360 | return -1; | |
361 | } | |
362 | ||
363 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); | |
364 | ||
365 | if (ret == -2) | |
366 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
367 | ||
368 | return ret; | |
369 | ||
370 | } | |
371 | ||
372 | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, | |
373 | const char *name, const char *value) | |
374 | { | |
375 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) { | |
376 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
377 | return -2; | |
378 | } | |
379 | if (strcmp(name, "digest") == 0) { | |
380 | const EVP_MD *md; | |
381 | if (value == NULL || (md = EVP_get_digestbyname(value)) == NULL) { | |
382 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST); | |
383 | return 0; | |
384 | } | |
385 | return EVP_PKEY_CTX_set_signature_md(ctx, md); | |
386 | } | |
387 | return ctx->pmeth->ctrl_str(ctx, name, value); | |
388 | } | |
389 | ||
390 | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) | |
391 | { | |
392 | return ctx->operation; | |
393 | } | |
394 | ||
395 | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) | |
396 | { | |
397 | ctx->keygen_info = dat; | |
398 | ctx->keygen_info_count = datlen; | |
399 | } | |
400 | ||
401 | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) | |
402 | { | |
403 | ctx->data = data; | |
404 | } | |
405 | ||
406 | void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx) | |
407 | { | |
408 | return ctx->data; | |
409 | } | |
410 | ||
411 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) | |
412 | { | |
413 | return ctx->pkey; | |
414 | } | |
415 | ||
416 | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) | |
417 | { | |
418 | return ctx->peerkey; | |
419 | } | |
420 | ||
421 | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) | |
422 | { | |
423 | ctx->app_data = data; | |
424 | } | |
425 | ||
426 | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) | |
427 | { | |
428 | return ctx->app_data; | |
429 | } | |
430 | ||
431 | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, | |
432 | int (*init) (EVP_PKEY_CTX *ctx)) | |
433 | { | |
434 | pmeth->init = init; | |
435 | } | |
436 | ||
437 | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, | |
438 | int (*copy) (EVP_PKEY_CTX *dst, | |
439 | EVP_PKEY_CTX *src)) | |
440 | { | |
441 | pmeth->copy = copy; | |
442 | } | |
443 | ||
444 | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, | |
445 | void (*cleanup) (EVP_PKEY_CTX *ctx)) | |
446 | { | |
447 | pmeth->cleanup = cleanup; | |
448 | } | |
449 | ||
450 | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, | |
451 | int (*paramgen_init) (EVP_PKEY_CTX *ctx), | |
452 | int (*paramgen) (EVP_PKEY_CTX *ctx, | |
453 | EVP_PKEY *pkey)) | |
454 | { | |
455 | pmeth->paramgen_init = paramgen_init; | |
456 | pmeth->paramgen = paramgen; | |
457 | } | |
458 | ||
459 | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, | |
460 | int (*keygen_init) (EVP_PKEY_CTX *ctx), | |
461 | int (*keygen) (EVP_PKEY_CTX *ctx, | |
462 | EVP_PKEY *pkey)) | |
463 | { | |
464 | pmeth->keygen_init = keygen_init; | |
465 | pmeth->keygen = keygen; | |
466 | } | |
467 | ||
468 | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, | |
469 | int (*sign_init) (EVP_PKEY_CTX *ctx), | |
470 | int (*sign) (EVP_PKEY_CTX *ctx, | |
471 | unsigned char *sig, size_t *siglen, | |
472 | const unsigned char *tbs, | |
473 | size_t tbslen)) | |
474 | { | |
475 | pmeth->sign_init = sign_init; | |
476 | pmeth->sign = sign; | |
477 | } | |
478 | ||
479 | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, | |
480 | int (*verify_init) (EVP_PKEY_CTX *ctx), | |
481 | int (*verify) (EVP_PKEY_CTX *ctx, | |
482 | const unsigned char *sig, | |
483 | size_t siglen, | |
484 | const unsigned char *tbs, | |
485 | size_t tbslen)) | |
486 | { | |
487 | pmeth->verify_init = verify_init; | |
488 | pmeth->verify = verify; | |
489 | } | |
490 | ||
491 | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, | |
492 | int (*verify_recover_init) (EVP_PKEY_CTX | |
493 | *ctx), | |
494 | int (*verify_recover) (EVP_PKEY_CTX | |
495 | *ctx, | |
496 | unsigned char | |
497 | *sig, | |
498 | size_t *siglen, | |
499 | const unsigned | |
500 | char *tbs, | |
501 | size_t tbslen)) | |
502 | { | |
503 | pmeth->verify_recover_init = verify_recover_init; | |
504 | pmeth->verify_recover = verify_recover; | |
505 | } | |
506 | ||
507 | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, | |
508 | int (*signctx_init) (EVP_PKEY_CTX *ctx, | |
509 | EVP_MD_CTX *mctx), | |
510 | int (*signctx) (EVP_PKEY_CTX *ctx, | |
511 | unsigned char *sig, | |
512 | size_t *siglen, | |
513 | EVP_MD_CTX *mctx)) | |
514 | { | |
515 | pmeth->signctx_init = signctx_init; | |
516 | pmeth->signctx = signctx; | |
517 | } | |
518 | ||
519 | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, | |
520 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, | |
521 | EVP_MD_CTX *mctx), | |
522 | int (*verifyctx) (EVP_PKEY_CTX *ctx, | |
523 | const unsigned char *sig, | |
524 | int siglen, | |
525 | EVP_MD_CTX *mctx)) | |
526 | { | |
527 | pmeth->verifyctx_init = verifyctx_init; | |
528 | pmeth->verifyctx = verifyctx; | |
529 | } | |
530 | ||
531 | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, | |
532 | int (*encrypt_init) (EVP_PKEY_CTX *ctx), | |
533 | int (*encryptfn) (EVP_PKEY_CTX *ctx, | |
534 | unsigned char *out, | |
535 | size_t *outlen, | |
536 | const unsigned char *in, | |
537 | size_t inlen)) | |
538 | { | |
539 | pmeth->encrypt_init = encrypt_init; | |
540 | pmeth->encrypt = encryptfn; | |
541 | } | |
542 | ||
543 | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, | |
544 | int (*decrypt_init) (EVP_PKEY_CTX *ctx), | |
545 | int (*decrypt) (EVP_PKEY_CTX *ctx, | |
546 | unsigned char *out, | |
547 | size_t *outlen, | |
548 | const unsigned char *in, | |
549 | size_t inlen)) | |
550 | { | |
551 | pmeth->decrypt_init = decrypt_init; | |
552 | pmeth->decrypt = decrypt; | |
553 | } | |
554 | ||
555 | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, | |
556 | int (*derive_init) (EVP_PKEY_CTX *ctx), | |
557 | int (*derive) (EVP_PKEY_CTX *ctx, | |
558 | unsigned char *key, | |
559 | size_t *keylen)) | |
560 | { | |
561 | pmeth->derive_init = derive_init; | |
562 | pmeth->derive = derive; | |
563 | } | |
564 | ||
565 | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |
566 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, | |
567 | void *p2), | |
568 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, | |
569 | const char *type, | |
570 | const char *value)) | |
571 | { | |
572 | pmeth->ctrl = ctrl; | |
573 | pmeth->ctrl_str = ctrl_str; | |
574 | } | |
575 | ||
576 | void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth, | |
577 | int (**pinit) (EVP_PKEY_CTX *ctx)) | |
578 | { | |
579 | *pinit = pmeth->init; | |
580 | } | |
581 | ||
582 | void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth, | |
583 | int (**pcopy) (EVP_PKEY_CTX *dst, | |
584 | EVP_PKEY_CTX *src)) | |
585 | { | |
586 | *pcopy = pmeth->copy; | |
587 | } | |
588 | ||
589 | void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth, | |
590 | void (**pcleanup) (EVP_PKEY_CTX *ctx)) | |
591 | { | |
592 | *pcleanup = pmeth->cleanup; | |
593 | } | |
594 | ||
595 | void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth, | |
596 | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), | |
597 | int (**pparamgen) (EVP_PKEY_CTX *ctx, | |
598 | EVP_PKEY *pkey)) | |
599 | { | |
600 | if (pparamgen_init) | |
601 | *pparamgen_init = pmeth->paramgen_init; | |
602 | if (pparamgen) | |
603 | *pparamgen = pmeth->paramgen; | |
604 | } | |
605 | ||
606 | void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth, | |
607 | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), | |
608 | int (**pkeygen) (EVP_PKEY_CTX *ctx, | |
609 | EVP_PKEY *pkey)) | |
610 | { | |
611 | if (pkeygen_init) | |
612 | *pkeygen_init = pmeth->keygen_init; | |
613 | if (pkeygen) | |
614 | *pkeygen = pmeth->keygen; | |
615 | } | |
616 | ||
617 | void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth, | |
618 | int (**psign_init) (EVP_PKEY_CTX *ctx), | |
619 | int (**psign) (EVP_PKEY_CTX *ctx, | |
620 | unsigned char *sig, size_t *siglen, | |
621 | const unsigned char *tbs, | |
622 | size_t tbslen)) | |
623 | { | |
624 | if (psign_init) | |
625 | *psign_init = pmeth->sign_init; | |
626 | if (psign) | |
627 | *psign = pmeth->sign; | |
628 | } | |
629 | ||
630 | void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth, | |
631 | int (**pverify_init) (EVP_PKEY_CTX *ctx), | |
632 | int (**pverify) (EVP_PKEY_CTX *ctx, | |
633 | const unsigned char *sig, | |
634 | size_t siglen, | |
635 | const unsigned char *tbs, | |
636 | size_t tbslen)) | |
637 | { | |
638 | if (pverify_init) | |
639 | *pverify_init = pmeth->verify_init; | |
640 | if (pverify) | |
641 | *pverify = pmeth->verify; | |
642 | } | |
643 | ||
644 | void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth, | |
645 | int (**pverify_recover_init) (EVP_PKEY_CTX | |
646 | *ctx), | |
647 | int (**pverify_recover) (EVP_PKEY_CTX | |
648 | *ctx, | |
649 | unsigned char | |
650 | *sig, | |
651 | size_t *siglen, | |
652 | const unsigned | |
653 | char *tbs, | |
654 | size_t tbslen)) | |
655 | { | |
656 | if (pverify_recover_init) | |
657 | *pverify_recover_init = pmeth->verify_recover_init; | |
658 | if (pverify_recover) | |
659 | *pverify_recover = pmeth->verify_recover; | |
660 | } | |
661 | ||
662 | void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth, | |
663 | int (**psignctx_init) (EVP_PKEY_CTX *ctx, | |
664 | EVP_MD_CTX *mctx), | |
665 | int (**psignctx) (EVP_PKEY_CTX *ctx, | |
666 | unsigned char *sig, | |
667 | size_t *siglen, | |
668 | EVP_MD_CTX *mctx)) | |
669 | { | |
670 | if (psignctx_init) | |
671 | *psignctx_init = pmeth->signctx_init; | |
672 | if (psignctx) | |
673 | *psignctx = pmeth->signctx; | |
674 | } | |
675 | ||
676 | void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth, | |
677 | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, | |
678 | EVP_MD_CTX *mctx), | |
679 | int (**pverifyctx) (EVP_PKEY_CTX *ctx, | |
680 | const unsigned char *sig, | |
681 | int siglen, | |
682 | EVP_MD_CTX *mctx)) | |
683 | { | |
684 | if (pverifyctx_init) | |
685 | *pverifyctx_init = pmeth->verifyctx_init; | |
686 | if (pverifyctx) | |
687 | *pverifyctx = pmeth->verifyctx; | |
688 | } | |
689 | ||
690 | void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth, | |
691 | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), | |
692 | int (**pencryptfn) (EVP_PKEY_CTX *ctx, | |
693 | unsigned char *out, | |
694 | size_t *outlen, | |
695 | const unsigned char *in, | |
696 | size_t inlen)) | |
697 | { | |
698 | if (pencrypt_init) | |
699 | *pencrypt_init = pmeth->encrypt_init; | |
700 | if (pencryptfn) | |
701 | *pencryptfn = pmeth->encrypt; | |
702 | } | |
703 | ||
704 | void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth, | |
705 | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), | |
706 | int (**pdecrypt) (EVP_PKEY_CTX *ctx, | |
707 | unsigned char *out, | |
708 | size_t *outlen, | |
709 | const unsigned char *in, | |
710 | size_t inlen)) | |
711 | { | |
712 | if (pdecrypt_init) | |
713 | *pdecrypt_init = pmeth->decrypt_init; | |
714 | if (pdecrypt) | |
715 | *pdecrypt = pmeth->decrypt; | |
716 | } | |
717 | ||
718 | void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth, | |
719 | int (**pderive_init) (EVP_PKEY_CTX *ctx), | |
720 | int (**pderive) (EVP_PKEY_CTX *ctx, | |
721 | unsigned char *key, | |
722 | size_t *keylen)) | |
723 | { | |
724 | if (pderive_init) | |
725 | *pderive_init = pmeth->derive_init; | |
726 | if (pderive) | |
727 | *pderive = pmeth->derive; | |
728 | } | |
729 | ||
730 | void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth, | |
731 | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, | |
732 | void *p2), | |
733 | int (**pctrl_str) (EVP_PKEY_CTX *ctx, | |
734 | const char *type, | |
735 | const char *value)) | |
736 | { | |
737 | if (pctrl) | |
738 | *pctrl = pmeth->ctrl; | |
739 | if (pctrl_str) | |
740 | *pctrl_str = pmeth->ctrl_str; | |
741 | } |