]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
b0edda11 | 2 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
0b6f3c66 | 3 | * |
62867571 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 | |
0b6f3c66 DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
b39fc560 | 12 | #include "internal/cryptlib.h" |
3c27208f | 13 | #include <openssl/engine.h> |
33bed28b | 14 | #include <openssl/evp.h> |
99119000 | 15 | #include <openssl/x509v3.h> |
5fe736e5 | 16 | #include "internal/asn1_int.h" |
27af42f9 | 17 | #include "internal/evp_int.h" |
99119000 | 18 | #include "internal/numbers.h" |
0b6f3c66 | 19 | |
0f113f3e | 20 | typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); |
5ce278a7 | 21 | |
df2ee0e2 | 22 | static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
0b6f3c66 | 23 | |
cefa762e | 24 | /* This array needs to be in order of NIDs */ |
0f113f3e | 25 | static const EVP_PKEY_METHOD *standard_methods[] = { |
d4f0339c | 26 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 27 | &rsa_pkey_meth, |
d4f0339c DSH |
28 | #endif |
29 | #ifndef OPENSSL_NO_DH | |
0f113f3e | 30 | &dh_pkey_meth, |
d4f0339c DSH |
31 | #endif |
32 | #ifndef OPENSSL_NO_DSA | |
0f113f3e | 33 | &dsa_pkey_meth, |
d4f0339c | 34 | #endif |
ef236ec3 | 35 | #ifndef OPENSSL_NO_EC |
0f113f3e | 36 | &ec_pkey_meth, |
ef236ec3 | 37 | #endif |
0f113f3e | 38 | &hmac_pkey_meth, |
b4a3aeeb | 39 | #ifndef OPENSSL_NO_CMAC |
0f113f3e | 40 | &cmac_pkey_meth, |
b4a3aeeb | 41 | #endif |
6577e008 DSH |
42 | #ifndef OPENSSL_NO_RSA |
43 | &rsa_pss_pkey_meth, | |
44 | #endif | |
afb14cda | 45 | #ifndef OPENSSL_NO_DH |
1eff3485 | 46 | &dhx_pkey_meth, |
cefa762e JB |
47 | #endif |
48 | #ifndef OPENSSL_NO_SCRYPT | |
49 | &scrypt_pkey_meth, | |
afb14cda | 50 | #endif |
aacfb134 | 51 | &tls1_prf_pkey_meth, |
262bd85f DSH |
52 | #ifndef OPENSSL_NO_EC |
53 | &ecx25519_pkey_meth, | |
13735cfe | 54 | &ecx448_pkey_meth, |
262bd85f | 55 | #endif |
52ad5b60 TS |
56 | &hkdf_pkey_meth, |
57 | #ifndef OPENSSL_NO_POLY1305 | |
58 | &poly1305_pkey_meth, | |
59 | #endif | |
3f5616d7 TS |
60 | #ifndef OPENSSL_NO_SIPHASH |
61 | &siphash_pkey_meth, | |
62 | #endif | |
42a3008a DSH |
63 | #ifndef OPENSSL_NO_EC |
64 | &ed25519_pkey_meth, | |
13735cfe | 65 | &ed448_pkey_meth, |
42a3008a | 66 | #endif |
ddb634fe JL |
67 | #ifndef OPENSSL_NO_SM2 |
68 | &sm2_pkey_meth, | |
69 | #endif | |
0f113f3e | 70 | }; |
0b6f3c66 | 71 | |
606f6c47 | 72 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, |
0f113f3e | 73 | pmeth); |
babb3798 | 74 | |
0f113f3e MC |
75 | static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, |
76 | const EVP_PKEY_METHOD *const *b) | |
77 | { | |
78 | return ((*a)->pkey_id - (*b)->pkey_id); | |
79 | } | |
0b6f3c66 | 80 | |
606f6c47 | 81 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, |
0f113f3e | 82 | pmeth); |
babb3798 | 83 | |
c9777d26 | 84 | const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) |
0f113f3e MC |
85 | { |
86 | EVP_PKEY_METHOD tmp; | |
87 | const EVP_PKEY_METHOD *t = &tmp, **ret; | |
88 | tmp.pkey_id = type; | |
89 | if (app_pkey_methods) { | |
90 | int idx; | |
91 | idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); | |
92 | if (idx >= 0) | |
93 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
94 | } | |
95 | ret = OBJ_bsearch_pmeth(&t, standard_methods, | |
96 | sizeof(standard_methods) / | |
97 | sizeof(EVP_PKEY_METHOD *)); | |
98 | if (!ret || !*ret) | |
99 | return NULL; | |
100 | return *ret; | |
101 | } | |
0b6f3c66 | 102 | |
f5cda4cb | 103 | static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) |
0f113f3e MC |
104 | { |
105 | EVP_PKEY_CTX *ret; | |
106 | const EVP_PKEY_METHOD *pmeth; | |
2f2e6b62 | 107 | |
0f113f3e | 108 | if (id == -1) { |
a6465b3f P |
109 | if (pkey == NULL) |
110 | return 0; | |
2f2e6b62 | 111 | id = pkey->type; |
0f113f3e | 112 | } |
a63bf2c5 | 113 | #ifndef OPENSSL_NO_ENGINE |
c2976edf | 114 | if (e == NULL && pkey != NULL) |
d19b01ad | 115 | e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine; |
0f113f3e MC |
116 | /* Try to find an ENGINE which implements this method */ |
117 | if (e) { | |
118 | if (!ENGINE_init(e)) { | |
119 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB); | |
120 | return NULL; | |
121 | } | |
c2976edf | 122 | } else { |
0f113f3e | 123 | e = ENGINE_get_pkey_meth_engine(id); |
c2976edf | 124 | } |
0f113f3e MC |
125 | |
126 | /* | |
0d4fb843 | 127 | * If an ENGINE handled this method look it up. Otherwise use internal |
0f113f3e MC |
128 | * tables. |
129 | */ | |
0f113f3e MC |
130 | if (e) |
131 | pmeth = ENGINE_get_pkey_meth(e, id); | |
132 | else | |
a63bf2c5 | 133 | #endif |
0f113f3e | 134 | pmeth = EVP_PKEY_meth_find(id); |
c9777d26 | 135 | |
0f113f3e | 136 | if (pmeth == NULL) { |
918a27fa DSH |
137 | #ifndef OPENSSL_NO_ENGINE |
138 | ENGINE_finish(e); | |
139 | #endif | |
0f113f3e MC |
140 | EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM); |
141 | return NULL; | |
142 | } | |
c9777d26 | 143 | |
64b25758 | 144 | ret = OPENSSL_zalloc(sizeof(*ret)); |
90945fa3 | 145 | if (ret == NULL) { |
a63bf2c5 | 146 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 147 | ENGINE_finish(e); |
a63bf2c5 | 148 | #endif |
0f113f3e MC |
149 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); |
150 | return NULL; | |
151 | } | |
152 | ret->engine = e; | |
153 | ret->pmeth = pmeth; | |
154 | ret->operation = EVP_PKEY_OP_UNDEFINED; | |
155 | ret->pkey = pkey; | |
a6465b3f | 156 | if (pkey != NULL) |
03273d61 | 157 | EVP_PKEY_up_ref(pkey); |
0f113f3e MC |
158 | |
159 | if (pmeth->init) { | |
160 | if (pmeth->init(ret) <= 0) { | |
83b4049a | 161 | ret->pmeth = NULL; |
0f113f3e MC |
162 | EVP_PKEY_CTX_free(ret); |
163 | return NULL; | |
164 | } | |
165 | } | |
166 | ||
167 | return ret; | |
168 | } | |
169 | ||
170 | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) | |
171 | { | |
172 | EVP_PKEY_METHOD *pmeth; | |
b4faea50 | 173 | |
b51bce94 | 174 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); |
3484236d F |
175 | if (pmeth == NULL) { |
176 | EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 177 | return NULL; |
3484236d | 178 | } |
0f113f3e | 179 | |
0f113f3e MC |
180 | pmeth->pkey_id = id; |
181 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | |
0f113f3e MC |
182 | return pmeth; |
183 | } | |
ba30bad5 | 184 | |
f830c68f | 185 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
0f113f3e MC |
186 | const EVP_PKEY_METHOD *meth) |
187 | { | |
188 | if (ppkey_id) | |
189 | *ppkey_id = meth->pkey_id; | |
190 | if (pflags) | |
191 | *pflags = meth->flags; | |
192 | } | |
f830c68f DSH |
193 | |
194 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | |
0f113f3e | 195 | { |
f830c68f | 196 | |
0f113f3e MC |
197 | dst->init = src->init; |
198 | dst->copy = src->copy; | |
199 | dst->cleanup = src->cleanup; | |
f830c68f | 200 | |
0f113f3e MC |
201 | dst->paramgen_init = src->paramgen_init; |
202 | dst->paramgen = src->paramgen; | |
f830c68f | 203 | |
0f113f3e MC |
204 | dst->keygen_init = src->keygen_init; |
205 | dst->keygen = src->keygen; | |
f830c68f | 206 | |
0f113f3e MC |
207 | dst->sign_init = src->sign_init; |
208 | dst->sign = src->sign; | |
f830c68f | 209 | |
0f113f3e MC |
210 | dst->verify_init = src->verify_init; |
211 | dst->verify = src->verify; | |
f830c68f | 212 | |
0f113f3e MC |
213 | dst->verify_recover_init = src->verify_recover_init; |
214 | dst->verify_recover = src->verify_recover; | |
f830c68f | 215 | |
0f113f3e MC |
216 | dst->signctx_init = src->signctx_init; |
217 | dst->signctx = src->signctx; | |
f830c68f | 218 | |
0f113f3e MC |
219 | dst->verifyctx_init = src->verifyctx_init; |
220 | dst->verifyctx = src->verifyctx; | |
f830c68f | 221 | |
0f113f3e MC |
222 | dst->encrypt_init = src->encrypt_init; |
223 | dst->encrypt = src->encrypt; | |
f830c68f | 224 | |
0f113f3e MC |
225 | dst->decrypt_init = src->decrypt_init; |
226 | dst->decrypt = src->decrypt; | |
f830c68f | 227 | |
0f113f3e MC |
228 | dst->derive_init = src->derive_init; |
229 | dst->derive = src->derive; | |
f830c68f | 230 | |
0f113f3e MC |
231 | dst->ctrl = src->ctrl; |
232 | dst->ctrl_str = src->ctrl_str; | |
2aee35d3 PY |
233 | |
234 | dst->check = src->check; | |
0f113f3e | 235 | } |
f830c68f | 236 | |
ba30bad5 | 237 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
238 | { |
239 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | |
240 | OPENSSL_free(pmeth); | |
241 | } | |
ba30bad5 | 242 | |
f5cda4cb | 243 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
0f113f3e MC |
244 | { |
245 | return int_ctx_new(pkey, e, -1); | |
246 | } | |
f5cda4cb DSH |
247 | |
248 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) | |
0f113f3e MC |
249 | { |
250 | return int_ctx_new(NULL, e, id); | |
251 | } | |
f5cda4cb | 252 | |
8bdcef40 | 253 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx) |
0f113f3e MC |
254 | { |
255 | EVP_PKEY_CTX *rctx; | |
256 | if (!pctx->pmeth || !pctx->pmeth->copy) | |
257 | return NULL; | |
c9777d26 | 258 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
259 | /* Make sure it's safe to copy a pkey context using an ENGINE */ |
260 | if (pctx->engine && !ENGINE_init(pctx->engine)) { | |
261 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); | |
262 | return 0; | |
263 | } | |
c9777d26 | 264 | #endif |
b4faea50 | 265 | rctx = OPENSSL_malloc(sizeof(*rctx)); |
3484236d F |
266 | if (rctx == NULL) { |
267 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 268 | return NULL; |
3484236d | 269 | } |
8bdcef40 | 270 | |
0f113f3e | 271 | rctx->pmeth = pctx->pmeth; |
c9777d26 | 272 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 273 | rctx->engine = pctx->engine; |
c9777d26 | 274 | #endif |
8bdcef40 | 275 | |
0f113f3e | 276 | if (pctx->pkey) |
03273d61 | 277 | EVP_PKEY_up_ref(pctx->pkey); |
944f8580 | 278 | |
0f113f3e | 279 | rctx->pkey = pctx->pkey; |
8bdcef40 | 280 | |
0f113f3e | 281 | if (pctx->peerkey) |
03273d61 | 282 | EVP_PKEY_up_ref(pctx->peerkey); |
944f8580 | 283 | |
0f113f3e | 284 | rctx->peerkey = pctx->peerkey; |
8bdcef40 | 285 | |
0f113f3e MC |
286 | rctx->data = NULL; |
287 | rctx->app_data = NULL; | |
288 | rctx->operation = pctx->operation; | |
8bdcef40 | 289 | |
0f113f3e MC |
290 | if (pctx->pmeth->copy(rctx, pctx) > 0) |
291 | return rctx; | |
8bdcef40 | 292 | |
83b4049a | 293 | rctx->pmeth = NULL; |
0f113f3e MC |
294 | EVP_PKEY_CTX_free(rctx); |
295 | return NULL; | |
8bdcef40 | 296 | |
0f113f3e | 297 | } |
8bdcef40 | 298 | |
ba30bad5 | 299 | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
300 | { |
301 | if (app_pkey_methods == NULL) { | |
302 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); | |
3484236d F |
303 | if (app_pkey_methods == NULL){ |
304 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 305 | return 0; |
3484236d | 306 | } |
0f113f3e | 307 | } |
3484236d F |
308 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
309 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 310 | return 0; |
3484236d | 311 | } |
0f113f3e MC |
312 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
313 | return 1; | |
314 | } | |
ba30bad5 | 315 | |
0822e89a PY |
316 | void evp_app_cleanup_int(void) |
317 | { | |
318 | if (app_pkey_methods != NULL) | |
319 | sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); | |
320 | } | |
321 | ||
322 | int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) | |
323 | { | |
324 | const EVP_PKEY_METHOD *ret; | |
325 | ||
326 | ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); | |
327 | ||
328 | return ret == NULL ? 0 : 1; | |
329 | } | |
330 | ||
48ed9c23 DSH |
331 | size_t EVP_PKEY_meth_get_count(void) |
332 | { | |
333 | size_t rv = OSSL_NELEM(standard_methods); | |
334 | ||
335 | if (app_pkey_methods) | |
336 | rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); | |
337 | return rv; | |
338 | } | |
339 | ||
340 | const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) | |
341 | { | |
342 | if (idx < OSSL_NELEM(standard_methods)) | |
343 | return standard_methods[idx]; | |
344 | if (app_pkey_methods == NULL) | |
345 | return NULL; | |
346 | idx -= OSSL_NELEM(standard_methods); | |
347 | if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) | |
348 | return NULL; | |
349 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
350 | } | |
351 | ||
5da98aa6 | 352 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
353 | { |
354 | if (ctx == NULL) | |
355 | return; | |
356 | if (ctx->pmeth && ctx->pmeth->cleanup) | |
357 | ctx->pmeth->cleanup(ctx); | |
c5ba2d99 RS |
358 | EVP_PKEY_free(ctx->pkey); |
359 | EVP_PKEY_free(ctx->peerkey); | |
c9777d26 | 360 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 361 | ENGINE_finish(ctx->engine); |
c9777d26 | 362 | #endif |
0f113f3e MC |
363 | OPENSSL_free(ctx); |
364 | } | |
5da98aa6 | 365 | |
0b6f3c66 | 366 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
0f113f3e MC |
367 | int cmd, int p1, void *p2) |
368 | { | |
369 | int ret; | |
370 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) { | |
371 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
372 | return -2; | |
373 | } | |
374 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) | |
375 | return -1; | |
376 | ||
377 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { | |
378 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); | |
379 | return -1; | |
380 | } | |
381 | ||
382 | if ((optype != -1) && !(ctx->operation & optype)) { | |
383 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); | |
384 | return -1; | |
385 | } | |
386 | ||
387 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); | |
388 | ||
389 | if (ret == -2) | |
390 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
391 | ||
392 | return ret; | |
393 | ||
394 | } | |
0b6f3c66 | 395 | |
cefa762e | 396 | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
64bf1016 | 397 | int cmd, uint64_t value) |
cefa762e JB |
398 | { |
399 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); | |
400 | } | |
401 | ||
4a3dc3c0 | 402 | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
403 | const char *name, const char *value) |
404 | { | |
405 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) { | |
406 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
407 | return -2; | |
408 | } | |
410877ba DSH |
409 | if (strcmp(name, "digest") == 0) |
410 | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, | |
411 | value); | |
0f113f3e MC |
412 | return ctx->pmeth->ctrl_str(ctx, name, value); |
413 | } | |
f5cda4cb | 414 | |
99119000 DSH |
415 | /* Utility functions to send a string of hex string to a ctrl */ |
416 | ||
417 | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) | |
418 | { | |
419 | size_t len; | |
420 | ||
421 | len = strlen(str); | |
422 | if (len > INT_MAX) | |
423 | return -1; | |
424 | return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str); | |
425 | } | |
426 | ||
427 | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) | |
428 | { | |
429 | unsigned char *bin; | |
430 | long binlen; | |
431 | int rv = -1; | |
432 | ||
14f051a0 | 433 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
99119000 DSH |
434 | if (bin == NULL) |
435 | return 0; | |
436 | if (binlen <= INT_MAX) | |
437 | rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); | |
438 | OPENSSL_free(bin); | |
439 | return rv; | |
440 | } | |
52ad523c | 441 | |
410877ba DSH |
442 | /* Pass a message digest to a ctrl */ |
443 | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) | |
444 | { | |
445 | const EVP_MD *m; | |
c82bafc5 | 446 | |
410877ba DSH |
447 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
448 | EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST); | |
449 | return 0; | |
450 | } | |
451 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); | |
452 | } | |
99119000 | 453 | |
b28dea4e | 454 | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
455 | { |
456 | return ctx->operation; | |
457 | } | |
b28dea4e DSH |
458 | |
459 | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) | |
0f113f3e MC |
460 | { |
461 | ctx->keygen_info = dat; | |
462 | ctx->keygen_info_count = datlen; | |
463 | } | |
b28dea4e | 464 | |
f5cda4cb | 465 | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
466 | { |
467 | ctx->data = data; | |
468 | } | |
f5cda4cb DSH |
469 | |
470 | void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
471 | { |
472 | return ctx->data; | |
473 | } | |
f5cda4cb | 474 | |
81cebb8b | 475 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
476 | { |
477 | return ctx->pkey; | |
478 | } | |
81cebb8b | 479 | |
0e1dba93 | 480 | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
481 | { |
482 | return ctx->peerkey; | |
483 | } | |
484 | ||
f5cda4cb | 485 | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
486 | { |
487 | ctx->app_data = data; | |
488 | } | |
f5cda4cb DSH |
489 | |
490 | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
491 | { |
492 | return ctx->app_data; | |
493 | } | |
ba30bad5 DSH |
494 | |
495 | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
496 | int (*init) (EVP_PKEY_CTX *ctx)) |
497 | { | |
498 | pmeth->init = init; | |
499 | } | |
8bdcef40 DSH |
500 | |
501 | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
502 | int (*copy) (EVP_PKEY_CTX *dst, |
503 | EVP_PKEY_CTX *src)) | |
504 | { | |
505 | pmeth->copy = copy; | |
506 | } | |
ba30bad5 DSH |
507 | |
508 | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
509 | void (*cleanup) (EVP_PKEY_CTX *ctx)) |
510 | { | |
511 | pmeth->cleanup = cleanup; | |
512 | } | |
ba30bad5 DSH |
513 | |
514 | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
515 | int (*paramgen_init) (EVP_PKEY_CTX *ctx), |
516 | int (*paramgen) (EVP_PKEY_CTX *ctx, | |
517 | EVP_PKEY *pkey)) | |
518 | { | |
519 | pmeth->paramgen_init = paramgen_init; | |
520 | pmeth->paramgen = paramgen; | |
521 | } | |
ba30bad5 DSH |
522 | |
523 | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
524 | int (*keygen_init) (EVP_PKEY_CTX *ctx), |
525 | int (*keygen) (EVP_PKEY_CTX *ctx, | |
526 | EVP_PKEY *pkey)) | |
527 | { | |
528 | pmeth->keygen_init = keygen_init; | |
529 | pmeth->keygen = keygen; | |
530 | } | |
ba30bad5 DSH |
531 | |
532 | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
533 | int (*sign_init) (EVP_PKEY_CTX *ctx), |
534 | int (*sign) (EVP_PKEY_CTX *ctx, | |
535 | unsigned char *sig, size_t *siglen, | |
536 | const unsigned char *tbs, | |
537 | size_t tbslen)) | |
538 | { | |
539 | pmeth->sign_init = sign_init; | |
540 | pmeth->sign = sign; | |
541 | } | |
ba30bad5 DSH |
542 | |
543 | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
544 | int (*verify_init) (EVP_PKEY_CTX *ctx), |
545 | int (*verify) (EVP_PKEY_CTX *ctx, | |
546 | const unsigned char *sig, | |
547 | size_t siglen, | |
548 | const unsigned char *tbs, | |
549 | size_t tbslen)) | |
550 | { | |
551 | pmeth->verify_init = verify_init; | |
552 | pmeth->verify = verify; | |
553 | } | |
ba30bad5 DSH |
554 | |
555 | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
556 | int (*verify_recover_init) (EVP_PKEY_CTX |
557 | *ctx), | |
558 | int (*verify_recover) (EVP_PKEY_CTX | |
559 | *ctx, | |
560 | unsigned char | |
561 | *sig, | |
562 | size_t *siglen, | |
563 | const unsigned | |
564 | char *tbs, | |
565 | size_t tbslen)) | |
566 | { | |
567 | pmeth->verify_recover_init = verify_recover_init; | |
568 | pmeth->verify_recover = verify_recover; | |
569 | } | |
ba30bad5 DSH |
570 | |
571 | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
572 | int (*signctx_init) (EVP_PKEY_CTX *ctx, |
573 | EVP_MD_CTX *mctx), | |
574 | int (*signctx) (EVP_PKEY_CTX *ctx, | |
575 | unsigned char *sig, | |
576 | size_t *siglen, | |
577 | EVP_MD_CTX *mctx)) | |
578 | { | |
579 | pmeth->signctx_init = signctx_init; | |
580 | pmeth->signctx = signctx; | |
581 | } | |
ba30bad5 DSH |
582 | |
583 | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
584 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, |
585 | EVP_MD_CTX *mctx), | |
586 | int (*verifyctx) (EVP_PKEY_CTX *ctx, | |
587 | const unsigned char *sig, | |
588 | int siglen, | |
589 | EVP_MD_CTX *mctx)) | |
590 | { | |
591 | pmeth->verifyctx_init = verifyctx_init; | |
592 | pmeth->verifyctx = verifyctx; | |
593 | } | |
ba30bad5 DSH |
594 | |
595 | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
596 | int (*encrypt_init) (EVP_PKEY_CTX *ctx), |
597 | int (*encryptfn) (EVP_PKEY_CTX *ctx, | |
598 | unsigned char *out, | |
599 | size_t *outlen, | |
600 | const unsigned char *in, | |
601 | size_t inlen)) | |
602 | { | |
603 | pmeth->encrypt_init = encrypt_init; | |
604 | pmeth->encrypt = encryptfn; | |
605 | } | |
ba30bad5 DSH |
606 | |
607 | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
608 | int (*decrypt_init) (EVP_PKEY_CTX *ctx), |
609 | int (*decrypt) (EVP_PKEY_CTX *ctx, | |
610 | unsigned char *out, | |
611 | size_t *outlen, | |
612 | const unsigned char *in, | |
613 | size_t inlen)) | |
614 | { | |
615 | pmeth->decrypt_init = decrypt_init; | |
616 | pmeth->decrypt = decrypt; | |
617 | } | |
ba30bad5 DSH |
618 | |
619 | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
620 | int (*derive_init) (EVP_PKEY_CTX *ctx), |
621 | int (*derive) (EVP_PKEY_CTX *ctx, | |
622 | unsigned char *key, | |
623 | size_t *keylen)) | |
624 | { | |
625 | pmeth->derive_init = derive_init; | |
626 | pmeth->derive = derive; | |
627 | } | |
ba30bad5 DSH |
628 | |
629 | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
630 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
631 | void *p2), | |
632 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, | |
633 | const char *type, | |
634 | const char *value)) | |
635 | { | |
636 | pmeth->ctrl = ctrl; | |
637 | pmeth->ctrl_str = ctrl_str; | |
638 | } | |
e7451ed1 | 639 | |
2aee35d3 PY |
640 | void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
641 | int (*check) (EVP_PKEY *pkey)) | |
642 | { | |
643 | pmeth->check = check; | |
644 | } | |
645 | ||
b0004708 PY |
646 | void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
647 | int (*check) (EVP_PKEY *pkey)) | |
648 | { | |
649 | pmeth->public_check = check; | |
650 | } | |
651 | ||
652 | void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, | |
653 | int (*check) (EVP_PKEY *pkey)) | |
654 | { | |
655 | pmeth->param_check = check; | |
656 | } | |
657 | ||
693be9a2 | 658 | void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
659 | int (**pinit) (EVP_PKEY_CTX *ctx)) |
660 | { | |
661 | *pinit = pmeth->init; | |
662 | } | |
663 | ||
693be9a2 | 664 | void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
665 | int (**pcopy) (EVP_PKEY_CTX *dst, |
666 | EVP_PKEY_CTX *src)) | |
667 | { | |
668 | *pcopy = pmeth->copy; | |
669 | } | |
670 | ||
693be9a2 | 671 | void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
672 | void (**pcleanup) (EVP_PKEY_CTX *ctx)) |
673 | { | |
674 | *pcleanup = pmeth->cleanup; | |
675 | } | |
676 | ||
693be9a2 | 677 | void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
678 | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), |
679 | int (**pparamgen) (EVP_PKEY_CTX *ctx, | |
680 | EVP_PKEY *pkey)) | |
681 | { | |
682 | if (pparamgen_init) | |
683 | *pparamgen_init = pmeth->paramgen_init; | |
684 | if (pparamgen) | |
685 | *pparamgen = pmeth->paramgen; | |
686 | } | |
687 | ||
693be9a2 | 688 | void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
689 | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), |
690 | int (**pkeygen) (EVP_PKEY_CTX *ctx, | |
691 | EVP_PKEY *pkey)) | |
692 | { | |
693 | if (pkeygen_init) | |
694 | *pkeygen_init = pmeth->keygen_init; | |
695 | if (pkeygen) | |
696 | *pkeygen = pmeth->keygen; | |
697 | } | |
698 | ||
693be9a2 | 699 | void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
700 | int (**psign_init) (EVP_PKEY_CTX *ctx), |
701 | int (**psign) (EVP_PKEY_CTX *ctx, | |
702 | unsigned char *sig, size_t *siglen, | |
703 | const unsigned char *tbs, | |
704 | size_t tbslen)) | |
705 | { | |
706 | if (psign_init) | |
707 | *psign_init = pmeth->sign_init; | |
708 | if (psign) | |
709 | *psign = pmeth->sign; | |
710 | } | |
711 | ||
693be9a2 | 712 | void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
713 | int (**pverify_init) (EVP_PKEY_CTX *ctx), |
714 | int (**pverify) (EVP_PKEY_CTX *ctx, | |
715 | const unsigned char *sig, | |
716 | size_t siglen, | |
717 | const unsigned char *tbs, | |
718 | size_t tbslen)) | |
719 | { | |
720 | if (pverify_init) | |
721 | *pverify_init = pmeth->verify_init; | |
722 | if (pverify) | |
723 | *pverify = pmeth->verify; | |
724 | } | |
725 | ||
693be9a2 | 726 | void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
727 | int (**pverify_recover_init) (EVP_PKEY_CTX |
728 | *ctx), | |
729 | int (**pverify_recover) (EVP_PKEY_CTX | |
730 | *ctx, | |
731 | unsigned char | |
732 | *sig, | |
733 | size_t *siglen, | |
734 | const unsigned | |
735 | char *tbs, | |
736 | size_t tbslen)) | |
737 | { | |
738 | if (pverify_recover_init) | |
739 | *pverify_recover_init = pmeth->verify_recover_init; | |
740 | if (pverify_recover) | |
741 | *pverify_recover = pmeth->verify_recover; | |
742 | } | |
743 | ||
693be9a2 | 744 | void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
745 | int (**psignctx_init) (EVP_PKEY_CTX *ctx, |
746 | EVP_MD_CTX *mctx), | |
747 | int (**psignctx) (EVP_PKEY_CTX *ctx, | |
748 | unsigned char *sig, | |
749 | size_t *siglen, | |
750 | EVP_MD_CTX *mctx)) | |
751 | { | |
752 | if (psignctx_init) | |
753 | *psignctx_init = pmeth->signctx_init; | |
754 | if (psignctx) | |
755 | *psignctx = pmeth->signctx; | |
756 | } | |
757 | ||
693be9a2 | 758 | void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
759 | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, |
760 | EVP_MD_CTX *mctx), | |
761 | int (**pverifyctx) (EVP_PKEY_CTX *ctx, | |
762 | const unsigned char *sig, | |
763 | int siglen, | |
764 | EVP_MD_CTX *mctx)) | |
765 | { | |
766 | if (pverifyctx_init) | |
767 | *pverifyctx_init = pmeth->verifyctx_init; | |
768 | if (pverifyctx) | |
769 | *pverifyctx = pmeth->verifyctx; | |
770 | } | |
771 | ||
693be9a2 | 772 | void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
773 | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), |
774 | int (**pencryptfn) (EVP_PKEY_CTX *ctx, | |
775 | unsigned char *out, | |
776 | size_t *outlen, | |
777 | const unsigned char *in, | |
778 | size_t inlen)) | |
779 | { | |
780 | if (pencrypt_init) | |
781 | *pencrypt_init = pmeth->encrypt_init; | |
782 | if (pencryptfn) | |
783 | *pencryptfn = pmeth->encrypt; | |
784 | } | |
785 | ||
693be9a2 | 786 | void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
787 | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), |
788 | int (**pdecrypt) (EVP_PKEY_CTX *ctx, | |
789 | unsigned char *out, | |
790 | size_t *outlen, | |
791 | const unsigned char *in, | |
792 | size_t inlen)) | |
793 | { | |
794 | if (pdecrypt_init) | |
795 | *pdecrypt_init = pmeth->decrypt_init; | |
796 | if (pdecrypt) | |
797 | *pdecrypt = pmeth->decrypt; | |
798 | } | |
799 | ||
693be9a2 | 800 | void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
801 | int (**pderive_init) (EVP_PKEY_CTX *ctx), |
802 | int (**pderive) (EVP_PKEY_CTX *ctx, | |
803 | unsigned char *key, | |
804 | size_t *keylen)) | |
805 | { | |
806 | if (pderive_init) | |
807 | *pderive_init = pmeth->derive_init; | |
808 | if (pderive) | |
809 | *pderive = pmeth->derive; | |
810 | } | |
811 | ||
693be9a2 | 812 | void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
813 | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
814 | void *p2), | |
815 | int (**pctrl_str) (EVP_PKEY_CTX *ctx, | |
816 | const char *type, | |
817 | const char *value)) | |
818 | { | |
819 | if (pctrl) | |
820 | *pctrl = pmeth->ctrl; | |
821 | if (pctrl_str) | |
822 | *pctrl_str = pmeth->ctrl_str; | |
823 | } | |
2aee35d3 | 824 | |
693be9a2 | 825 | void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
2aee35d3 PY |
826 | int (**pcheck) (EVP_PKEY *pkey)) |
827 | { | |
828 | if (*pcheck) | |
829 | *pcheck = pmeth->check; | |
830 | } | |
b0004708 | 831 | |
693be9a2 | 832 | void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
833 | int (**pcheck) (EVP_PKEY *pkey)) |
834 | { | |
835 | if (*pcheck) | |
836 | *pcheck = pmeth->public_check; | |
837 | } | |
838 | ||
693be9a2 | 839 | void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
840 | int (**pcheck) (EVP_PKEY *pkey)) |
841 | { | |
842 | if (*pcheck) | |
843 | *pcheck = pmeth->param_check; | |
844 | } |