]>
Commit | Line | Data |
---|---|---|
e683582b SL |
1 | /* |
2 | * Copyright 2006-2016 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 <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <openssl/objects.h> | |
13 | #include <openssl/evp.h> | |
14 | #include "internal/cryptlib.h" | |
15 | #include "crypto/evp.h" | |
16 | #include "internal/provider.h" | |
17 | #include "evp_local.h" | |
18 | ||
19 | static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov) | |
20 | { | |
21 | EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE)); | |
22 | ||
23 | if (signature == NULL) { | |
24 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | |
25 | return NULL; | |
26 | } | |
27 | ||
28 | signature->lock = CRYPTO_THREAD_lock_new(); | |
29 | if (signature->lock == NULL) { | |
30 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | |
31 | OPENSSL_free(signature); | |
32 | return NULL; | |
33 | } | |
34 | signature->prov = prov; | |
35 | ossl_provider_up_ref(prov); | |
36 | signature->refcnt = 1; | |
37 | ||
38 | return signature; | |
39 | } | |
40 | ||
41 | static void *evp_signature_from_dispatch(int name_id, | |
42 | const OSSL_DISPATCH *fns, | |
43 | OSSL_PROVIDER *prov) | |
44 | { | |
45 | EVP_SIGNATURE *signature = NULL; | |
46 | int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0; | |
47 | int digsignfncnt = 0, digverifyfncnt = 0; | |
48 | int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0; | |
49 | ||
50 | if ((signature = evp_signature_new(prov)) == NULL) { | |
51 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | |
52 | goto err; | |
53 | } | |
54 | ||
55 | signature->name_id = name_id; | |
56 | ||
57 | for (; fns->function_id != 0; fns++) { | |
58 | switch (fns->function_id) { | |
59 | case OSSL_FUNC_SIGNATURE_NEWCTX: | |
60 | if (signature->newctx != NULL) | |
61 | break; | |
62 | signature->newctx = OSSL_get_OP_signature_newctx(fns); | |
63 | ctxfncnt++; | |
64 | break; | |
65 | case OSSL_FUNC_SIGNATURE_SIGN_INIT: | |
66 | if (signature->sign_init != NULL) | |
67 | break; | |
68 | signature->sign_init = OSSL_get_OP_signature_sign_init(fns); | |
69 | signfncnt++; | |
70 | break; | |
71 | case OSSL_FUNC_SIGNATURE_SIGN: | |
72 | if (signature->sign != NULL) | |
73 | break; | |
74 | signature->sign = OSSL_get_OP_signature_sign(fns); | |
75 | signfncnt++; | |
76 | break; | |
77 | case OSSL_FUNC_SIGNATURE_VERIFY_INIT: | |
78 | if (signature->verify_init != NULL) | |
79 | break; | |
80 | signature->verify_init = OSSL_get_OP_signature_verify_init(fns); | |
81 | verifyfncnt++; | |
82 | break; | |
83 | case OSSL_FUNC_SIGNATURE_VERIFY: | |
84 | if (signature->verify != NULL) | |
85 | break; | |
86 | signature->verify = OSSL_get_OP_signature_verify(fns); | |
87 | verifyfncnt++; | |
88 | break; | |
89 | case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT: | |
90 | if (signature->verify_recover_init != NULL) | |
91 | break; | |
92 | signature->verify_recover_init | |
93 | = OSSL_get_OP_signature_verify_recover_init(fns); | |
94 | verifyrecfncnt++; | |
95 | break; | |
96 | case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER: | |
97 | if (signature->verify_recover != NULL) | |
98 | break; | |
99 | signature->verify_recover | |
100 | = OSSL_get_OP_signature_verify_recover(fns); | |
101 | verifyrecfncnt++; | |
102 | break; | |
103 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT: | |
104 | if (signature->digest_sign_init != NULL) | |
105 | break; | |
106 | signature->digest_sign_init | |
107 | = OSSL_get_OP_signature_digest_sign_init(fns); | |
e683582b SL |
108 | break; |
109 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE: | |
110 | if (signature->digest_sign_update != NULL) | |
111 | break; | |
112 | signature->digest_sign_update | |
113 | = OSSL_get_OP_signature_digest_sign_update(fns); | |
114 | digsignfncnt++; | |
115 | break; | |
116 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL: | |
117 | if (signature->digest_sign_final != NULL) | |
118 | break; | |
119 | signature->digest_sign_final | |
120 | = OSSL_get_OP_signature_digest_sign_final(fns); | |
121 | digsignfncnt++; | |
122 | break; | |
eea1e780 MC |
123 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN: |
124 | if (signature->digest_sign != NULL) | |
125 | break; | |
126 | signature->digest_sign | |
127 | = OSSL_get_OP_signature_digest_sign(fns); | |
128 | break; | |
e683582b SL |
129 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT: |
130 | if (signature->digest_verify_init != NULL) | |
131 | break; | |
132 | signature->digest_verify_init | |
133 | = OSSL_get_OP_signature_digest_verify_init(fns); | |
e683582b SL |
134 | break; |
135 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE: | |
136 | if (signature->digest_verify_update != NULL) | |
137 | break; | |
138 | signature->digest_verify_update | |
139 | = OSSL_get_OP_signature_digest_verify_update(fns); | |
140 | digverifyfncnt++; | |
141 | break; | |
142 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL: | |
143 | if (signature->digest_verify_final != NULL) | |
144 | break; | |
145 | signature->digest_verify_final | |
146 | = OSSL_get_OP_signature_digest_verify_final(fns); | |
147 | digverifyfncnt++; | |
148 | break; | |
eea1e780 MC |
149 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY: |
150 | if (signature->digest_verify != NULL) | |
151 | break; | |
152 | signature->digest_verify | |
153 | = OSSL_get_OP_signature_digest_verify(fns); | |
154 | break; | |
e683582b SL |
155 | case OSSL_FUNC_SIGNATURE_FREECTX: |
156 | if (signature->freectx != NULL) | |
157 | break; | |
158 | signature->freectx = OSSL_get_OP_signature_freectx(fns); | |
159 | ctxfncnt++; | |
160 | break; | |
161 | case OSSL_FUNC_SIGNATURE_DUPCTX: | |
162 | if (signature->dupctx != NULL) | |
163 | break; | |
164 | signature->dupctx = OSSL_get_OP_signature_dupctx(fns); | |
165 | break; | |
166 | case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS: | |
167 | if (signature->get_ctx_params != NULL) | |
168 | break; | |
169 | signature->get_ctx_params | |
170 | = OSSL_get_OP_signature_get_ctx_params(fns); | |
171 | gparamfncnt++; | |
172 | break; | |
173 | case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS: | |
174 | if (signature->gettable_ctx_params != NULL) | |
175 | break; | |
176 | signature->gettable_ctx_params | |
177 | = OSSL_get_OP_signature_gettable_ctx_params(fns); | |
178 | gparamfncnt++; | |
179 | break; | |
180 | case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS: | |
181 | if (signature->set_ctx_params != NULL) | |
182 | break; | |
183 | signature->set_ctx_params | |
184 | = OSSL_get_OP_signature_set_ctx_params(fns); | |
185 | sparamfncnt++; | |
186 | break; | |
187 | case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS: | |
188 | if (signature->settable_ctx_params != NULL) | |
189 | break; | |
190 | signature->settable_ctx_params | |
191 | = OSSL_get_OP_signature_settable_ctx_params(fns); | |
192 | sparamfncnt++; | |
193 | break; | |
194 | case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS: | |
195 | if (signature->get_ctx_md_params != NULL) | |
196 | break; | |
197 | signature->get_ctx_md_params | |
198 | = OSSL_get_OP_signature_get_ctx_md_params(fns); | |
199 | gmdparamfncnt++; | |
200 | break; | |
201 | case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS: | |
202 | if (signature->gettable_ctx_md_params != NULL) | |
203 | break; | |
204 | signature->gettable_ctx_md_params | |
205 | = OSSL_get_OP_signature_gettable_ctx_md_params(fns); | |
206 | gmdparamfncnt++; | |
207 | break; | |
208 | case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS: | |
209 | if (signature->set_ctx_md_params != NULL) | |
210 | break; | |
211 | signature->set_ctx_md_params | |
212 | = OSSL_get_OP_signature_set_ctx_md_params(fns); | |
213 | smdparamfncnt++; | |
214 | break; | |
215 | case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS: | |
216 | if (signature->settable_ctx_md_params != NULL) | |
217 | break; | |
218 | signature->settable_ctx_md_params | |
219 | = OSSL_get_OP_signature_settable_ctx_md_params(fns); | |
220 | smdparamfncnt++; | |
221 | break; | |
222 | } | |
223 | } | |
224 | if (ctxfncnt != 2 | |
225 | || (signfncnt == 0 | |
226 | && verifyfncnt == 0 | |
227 | && verifyrecfncnt == 0 | |
228 | && digsignfncnt == 0 | |
eea1e780 MC |
229 | && digverifyfncnt == 0 |
230 | && signature->digest_sign == NULL | |
231 | && signature->digest_verify == NULL) | |
e683582b SL |
232 | || (signfncnt != 0 && signfncnt != 2) |
233 | || (verifyfncnt != 0 && verifyfncnt != 2) | |
234 | || (verifyrecfncnt != 0 && verifyrecfncnt != 2) | |
eea1e780 MC |
235 | || (digsignfncnt != 0 && digsignfncnt != 2) |
236 | || (digsignfncnt == 2 && signature->digest_sign_init == NULL) | |
237 | || (digverifyfncnt != 0 && digverifyfncnt != 2) | |
238 | || (digverifyfncnt == 2 && signature->digest_verify_init == NULL) | |
239 | || (signature->digest_sign != NULL | |
240 | && signature->digest_sign_init == NULL) | |
241 | || (signature->digest_verify != NULL | |
242 | && signature->digest_verify_init == NULL) | |
e683582b SL |
243 | || (gparamfncnt != 0 && gparamfncnt != 2) |
244 | || (sparamfncnt != 0 && sparamfncnt != 2) | |
245 | || (gmdparamfncnt != 0 && gmdparamfncnt != 2) | |
246 | || (smdparamfncnt != 0 && smdparamfncnt != 2)) { | |
247 | /* | |
248 | * In order to be a consistent set of functions we must have at least | |
249 | * a set of context functions (newctx and freectx) as well as a set of | |
250 | * "signature" functions: | |
251 | * (sign_init, sign) or | |
252 | * (verify_init verify) or | |
253 | * (verify_recover_init, verify_recover) or | |
254 | * (digest_sign_init, digest_sign_update, digest_sign_final) or | |
eea1e780 MC |
255 | * (digest_verify_init, digest_verify_update, digest_verify_final) or |
256 | * (digest_sign_init, digest_sign) or | |
257 | * (digest_verify_init, digest_verify). | |
e683582b SL |
258 | * |
259 | * set_ctx_params and settable_ctx_params are optional, but if one of | |
260 | * them is present then the other one must also be present. The same | |
261 | * applies to get_ctx_params and gettable_ctx_params. The same rules | |
262 | * apply to the "md_params" functions. The dupctx function is optional. | |
263 | */ | |
264 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); | |
265 | goto err; | |
266 | } | |
267 | ||
268 | return signature; | |
269 | err: | |
270 | EVP_SIGNATURE_free(signature); | |
271 | return NULL; | |
272 | } | |
273 | ||
274 | void EVP_SIGNATURE_free(EVP_SIGNATURE *signature) | |
275 | { | |
276 | if (signature != NULL) { | |
277 | int i; | |
278 | ||
279 | CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock); | |
280 | if (i > 0) | |
281 | return; | |
282 | ossl_provider_free(signature->prov); | |
283 | CRYPTO_THREAD_lock_free(signature->lock); | |
284 | OPENSSL_free(signature); | |
285 | } | |
286 | } | |
287 | ||
288 | int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature) | |
289 | { | |
290 | int ref = 0; | |
291 | ||
292 | CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock); | |
293 | return 1; | |
294 | } | |
295 | ||
296 | OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature) | |
297 | { | |
298 | return signature->prov; | |
299 | } | |
300 | ||
301 | EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm, | |
302 | const char *properties) | |
303 | { | |
304 | return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties, | |
305 | evp_signature_from_dispatch, | |
306 | (int (*)(void *))EVP_SIGNATURE_up_ref, | |
307 | (void (*)(void *))EVP_SIGNATURE_free); | |
308 | } | |
309 | ||
310 | int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name) | |
311 | { | |
e4a1d023 | 312 | return evp_is_a(signature->prov, signature->name_id, NULL, name); |
e683582b SL |
313 | } |
314 | ||
315 | int EVP_SIGNATURE_number(const EVP_SIGNATURE *signature) | |
316 | { | |
317 | return signature->name_id; | |
318 | } | |
319 | ||
320 | void EVP_SIGNATURE_do_all_provided(OPENSSL_CTX *libctx, | |
321 | void (*fn)(EVP_SIGNATURE *signature, | |
322 | void *arg), | |
323 | void *arg) | |
324 | { | |
325 | evp_generic_do_all(libctx, OSSL_OP_SIGNATURE, | |
326 | (void (*)(void *, void *))fn, arg, | |
327 | evp_signature_from_dispatch, | |
328 | (void (*)(void *))EVP_SIGNATURE_free); | |
329 | } | |
330 | ||
331 | ||
332 | void EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature, | |
333 | void (*fn)(const char *name, void *data), | |
334 | void *data) | |
335 | { | |
336 | if (signature->prov != NULL) | |
337 | evp_names_do_all(signature->prov, signature->name_id, fn, data); | |
338 | } | |
339 | ||
340 | static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation) | |
341 | { | |
342 | int ret = 0; | |
343 | void *provkey = NULL; | |
344 | EVP_SIGNATURE *signature = NULL; | |
f6aa5774 RL |
345 | EVP_KEYMGMT *tmp_keymgmt = NULL; |
346 | const char *supported_sig = NULL; | |
e683582b SL |
347 | |
348 | if (ctx == NULL) { | |
349 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
350 | return -2; | |
351 | } | |
352 | ||
353 | evp_pkey_ctx_free_old_ops(ctx); | |
354 | ctx->operation = operation; | |
355 | ||
0b9dd384 RL |
356 | /* |
357 | * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark() | |
358 | * calls can be removed. | |
359 | */ | |
360 | ERR_set_mark(); | |
361 | ||
4b9e90f4 | 362 | if (ctx->keymgmt == NULL) |
e683582b SL |
363 | goto legacy; |
364 | ||
3c6ed955 RL |
365 | /* |
366 | * Ensure that the key is provided, either natively, or as a cached export. | |
367 | * If not, go legacy | |
368 | */ | |
f6aa5774 | 369 | tmp_keymgmt = ctx->keymgmt; |
3c6ed955 RL |
370 | provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
371 | &tmp_keymgmt, ctx->propquery); | |
d0ddf9b4 | 372 | if (tmp_keymgmt == NULL) |
f6aa5774 RL |
373 | goto legacy; |
374 | if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { | |
0b9dd384 | 375 | ERR_clear_last_mark(); |
f6aa5774 RL |
376 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
377 | goto err; | |
e683582b | 378 | } |
f6aa5774 RL |
379 | EVP_KEYMGMT_free(ctx->keymgmt); |
380 | ctx->keymgmt = tmp_keymgmt; | |
381 | ||
382 | if (ctx->keymgmt->query_operation_name != NULL) | |
383 | supported_sig = ctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE); | |
384 | ||
385 | /* | |
386 | * If we didn't get a supported sig, assume there is one with the | |
387 | * same name as the key type. | |
388 | */ | |
389 | if (supported_sig == NULL) | |
390 | supported_sig = ctx->keytype; | |
391 | ||
392 | /* | |
393 | * Because we cleared out old ops, we shouldn't need to worry about | |
394 | * checking if signature is already there. | |
395 | */ | |
396 | signature = | |
397 | EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery); | |
398 | ||
399 | if (signature == NULL | |
e683582b SL |
400 | || (EVP_KEYMGMT_provider(ctx->keymgmt) |
401 | != EVP_SIGNATURE_provider(signature))) { | |
402 | /* | |
0b9dd384 RL |
403 | * We don't need to free ctx->keymgmt here, as it's not necessarily |
404 | * tied to this operation. It will be freed by EVP_PKEY_CTX_free(). | |
e683582b SL |
405 | */ |
406 | EVP_SIGNATURE_free(signature); | |
407 | goto legacy; | |
408 | } | |
409 | ||
0b9dd384 RL |
410 | /* |
411 | * TODO remove this when legacy is gone | |
412 | * If we don't have the full support we need with provided methods, | |
413 | * let's go see if legacy does. | |
414 | */ | |
415 | ERR_pop_to_mark(); | |
416 | ||
417 | /* No more legacy from here down to legacy: */ | |
418 | ||
e683582b | 419 | ctx->op.sig.signature = signature; |
e683582b SL |
420 | ctx->op.sig.sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov)); |
421 | if (ctx->op.sig.sigprovctx == NULL) { | |
422 | /* The provider key can stay in the cache */ | |
423 | EVPerr(0, EVP_R_INITIALIZATION_ERROR); | |
424 | goto err; | |
425 | } | |
426 | ||
427 | switch (operation) { | |
428 | case EVP_PKEY_OP_SIGN: | |
429 | if (signature->sign_init == NULL) { | |
430 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
431 | ret = -2; | |
432 | goto err; | |
433 | } | |
434 | ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey); | |
435 | break; | |
436 | case EVP_PKEY_OP_VERIFY: | |
437 | if (signature->verify_init == NULL) { | |
438 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
439 | ret = -2; | |
440 | goto err; | |
441 | } | |
442 | ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey); | |
443 | break; | |
444 | case EVP_PKEY_OP_VERIFYRECOVER: | |
445 | if (signature->verify_recover_init == NULL) { | |
446 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
447 | ret = -2; | |
448 | goto err; | |
449 | } | |
450 | ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey); | |
451 | break; | |
452 | default: | |
453 | EVPerr(0, EVP_R_INITIALIZATION_ERROR); | |
454 | goto err; | |
455 | } | |
456 | ||
457 | if (ret <= 0) { | |
458 | signature->freectx(ctx->op.sig.sigprovctx); | |
459 | ctx->op.sig.sigprovctx = NULL; | |
460 | goto err; | |
461 | } | |
462 | return 1; | |
463 | ||
464 | legacy: | |
0b9dd384 RL |
465 | /* |
466 | * TODO remove this when legacy is gone | |
467 | * If we don't have the full support we need with provided methods, | |
468 | * let's go see if legacy does. | |
469 | */ | |
470 | ERR_pop_to_mark(); | |
471 | ||
e683582b SL |
472 | if (ctx->pmeth == NULL |
473 | || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL) | |
474 | || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL) | |
475 | || (operation == EVP_PKEY_OP_VERIFYRECOVER | |
476 | && ctx->pmeth->verify_recover == NULL)) { | |
477 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
478 | return -2; | |
479 | } | |
480 | ||
481 | switch (operation) { | |
482 | case EVP_PKEY_OP_SIGN: | |
483 | if (ctx->pmeth->sign_init == NULL) | |
484 | return 1; | |
485 | ret = ctx->pmeth->sign_init(ctx); | |
486 | break; | |
487 | case EVP_PKEY_OP_VERIFY: | |
488 | if (ctx->pmeth->verify_init == NULL) | |
489 | return 1; | |
490 | ret = ctx->pmeth->verify_init(ctx); | |
491 | break; | |
492 | case EVP_PKEY_OP_VERIFYRECOVER: | |
493 | if (ctx->pmeth->verify_recover_init == NULL) | |
494 | return 1; | |
495 | ret = ctx->pmeth->verify_recover_init(ctx); | |
496 | break; | |
497 | default: | |
498 | EVPerr(0, EVP_R_INITIALIZATION_ERROR); | |
499 | goto err; | |
500 | } | |
501 | if (ret <= 0) | |
502 | goto err; | |
503 | return ret; | |
504 | ||
505 | err: | |
506 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
507 | return ret; | |
508 | } | |
509 | ||
510 | int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) | |
511 | { | |
512 | return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN); | |
513 | } | |
514 | ||
515 | int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, | |
516 | unsigned char *sig, size_t *siglen, | |
517 | const unsigned char *tbs, size_t tbslen) | |
518 | { | |
519 | int ret; | |
520 | ||
521 | if (ctx == NULL) { | |
522 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
523 | return -2; | |
524 | } | |
525 | ||
526 | if (ctx->operation != EVP_PKEY_OP_SIGN) { | |
527 | EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED); | |
528 | return -1; | |
529 | } | |
530 | ||
531 | if (ctx->op.sig.sigprovctx == NULL) | |
532 | goto legacy; | |
533 | ||
534 | ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen, | |
535 | SIZE_MAX, tbs, tbslen); | |
536 | ||
537 | return ret; | |
538 | legacy: | |
539 | ||
540 | if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) { | |
541 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
542 | return -2; | |
543 | } | |
544 | ||
545 | M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN) | |
546 | return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen); | |
547 | } | |
548 | ||
549 | int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) | |
550 | { | |
551 | return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY); | |
552 | } | |
553 | ||
554 | int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, | |
555 | const unsigned char *sig, size_t siglen, | |
556 | const unsigned char *tbs, size_t tbslen) | |
557 | { | |
558 | int ret; | |
559 | ||
560 | if (ctx == NULL) { | |
561 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
562 | return -2; | |
563 | } | |
564 | ||
565 | if (ctx->operation != EVP_PKEY_OP_VERIFY) { | |
566 | EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED); | |
567 | return -1; | |
568 | } | |
569 | ||
570 | if (ctx->op.sig.sigprovctx == NULL) | |
571 | goto legacy; | |
572 | ||
573 | ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen, | |
574 | tbs, tbslen); | |
575 | ||
576 | return ret; | |
577 | legacy: | |
578 | if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) { | |
579 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
580 | return -2; | |
581 | } | |
582 | ||
583 | return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen); | |
584 | } | |
585 | ||
586 | int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) | |
587 | { | |
588 | return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER); | |
589 | } | |
590 | ||
591 | int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, | |
592 | unsigned char *rout, size_t *routlen, | |
593 | const unsigned char *sig, size_t siglen) | |
594 | { | |
595 | int ret; | |
596 | ||
597 | if (ctx == NULL) { | |
598 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
599 | return -2; | |
600 | } | |
601 | ||
602 | if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) { | |
603 | EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED); | |
604 | return -1; | |
605 | } | |
606 | ||
607 | if (ctx->op.sig.sigprovctx == NULL) | |
608 | goto legacy; | |
609 | ||
610 | ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout, | |
611 | routlen, | |
612 | (rout == NULL ? 0 : *routlen), | |
613 | sig, siglen); | |
614 | return ret; | |
615 | legacy: | |
616 | if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) { | |
617 | EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
618 | return -2; | |
619 | } | |
620 | M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER) | |
621 | return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen); | |
622 | } |