/src/openssl/crypto/evp/signature.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2025 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 <openssl/err.h> |
11 | | #include <stdio.h> |
12 | | #include <stdlib.h> |
13 | | #include <openssl/core_names.h> |
14 | | #include <openssl/objects.h> |
15 | | #include <openssl/evp.h> |
16 | | #include "internal/numbers.h" /* includes SIZE_MAX */ |
17 | | #include "internal/cryptlib.h" |
18 | | #include "internal/provider.h" |
19 | | #include "internal/core.h" |
20 | | #include "crypto/evp.h" |
21 | | #include "evp_local.h" |
22 | | |
23 | | static void evp_signature_free(void *data) |
24 | 0 | { |
25 | 0 | EVP_SIGNATURE_free(data); |
26 | 0 | } |
27 | | |
28 | | static int evp_signature_up_ref(void *data) |
29 | 0 | { |
30 | 0 | return EVP_SIGNATURE_up_ref(data); |
31 | 0 | } |
32 | | |
33 | | static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov) |
34 | 0 | { |
35 | 0 | EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE)); |
36 | |
|
37 | 0 | if (signature == NULL) |
38 | 0 | return NULL; |
39 | | |
40 | 0 | if (!CRYPTO_NEW_REF(&signature->refcnt, 1) |
41 | 0 | || !ossl_provider_up_ref(prov)) { |
42 | 0 | CRYPTO_FREE_REF(&signature->refcnt); |
43 | 0 | OPENSSL_free(signature); |
44 | 0 | return NULL; |
45 | 0 | } |
46 | | |
47 | 0 | signature->prov = prov; |
48 | |
|
49 | 0 | return signature; |
50 | 0 | } |
51 | | |
52 | | static void *evp_signature_from_algorithm(int name_id, |
53 | | const OSSL_ALGORITHM *algodef, |
54 | | OSSL_PROVIDER *prov) |
55 | 0 | { |
56 | 0 | const OSSL_DISPATCH *fns = algodef->implementation; |
57 | 0 | EVP_SIGNATURE *signature = NULL; |
58 | 0 | const char *desc; |
59 | | /* Counts newctx / freectx */ |
60 | 0 | int ctxfncnt = 0; |
61 | | /* Counts all init functions */ |
62 | 0 | int initfncnt = 0; |
63 | | /* Counts all parameter functions */ |
64 | 0 | int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0; |
65 | 0 | int valid = 0; |
66 | |
|
67 | 0 | if ((signature = evp_signature_new(prov)) == NULL) { |
68 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
69 | 0 | goto err; |
70 | 0 | } |
71 | | |
72 | 0 | signature->name_id = name_id; |
73 | 0 | if ((signature->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
74 | 0 | goto err; |
75 | 0 | signature->description = algodef->algorithm_description; |
76 | 0 | desc = signature->description != NULL ? signature->description : ""; |
77 | |
|
78 | 0 | for (; fns->function_id != 0; fns++) { |
79 | 0 | switch (fns->function_id) { |
80 | 0 | case OSSL_FUNC_SIGNATURE_NEWCTX: |
81 | 0 | if (signature->newctx != NULL) |
82 | 0 | break; |
83 | 0 | signature->newctx = OSSL_FUNC_signature_newctx(fns); |
84 | 0 | ctxfncnt++; |
85 | 0 | break; |
86 | 0 | case OSSL_FUNC_SIGNATURE_SIGN_INIT: |
87 | 0 | if (signature->sign_init != NULL) |
88 | 0 | break; |
89 | 0 | signature->sign_init = OSSL_FUNC_signature_sign_init(fns); |
90 | 0 | initfncnt++; |
91 | 0 | break; |
92 | 0 | case OSSL_FUNC_SIGNATURE_SIGN: |
93 | 0 | if (signature->sign != NULL) |
94 | 0 | break; |
95 | 0 | signature->sign = OSSL_FUNC_signature_sign(fns); |
96 | 0 | break; |
97 | 0 | case OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT: |
98 | 0 | if (signature->sign_message_init != NULL) |
99 | 0 | break; |
100 | 0 | signature->sign_message_init |
101 | 0 | = OSSL_FUNC_signature_sign_message_init(fns); |
102 | 0 | initfncnt++; |
103 | 0 | break; |
104 | 0 | case OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE: |
105 | 0 | if (signature->sign_message_update != NULL) |
106 | 0 | break; |
107 | 0 | signature->sign_message_update |
108 | 0 | = OSSL_FUNC_signature_sign_message_update(fns); |
109 | 0 | break; |
110 | 0 | case OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL: |
111 | 0 | if (signature->sign_message_final != NULL) |
112 | 0 | break; |
113 | 0 | signature->sign_message_final |
114 | 0 | = OSSL_FUNC_signature_sign_message_final(fns); |
115 | 0 | break; |
116 | 0 | case OSSL_FUNC_SIGNATURE_VERIFY_INIT: |
117 | 0 | if (signature->verify_init != NULL) |
118 | 0 | break; |
119 | 0 | signature->verify_init = OSSL_FUNC_signature_verify_init(fns); |
120 | 0 | initfncnt++; |
121 | 0 | break; |
122 | 0 | case OSSL_FUNC_SIGNATURE_VERIFY: |
123 | 0 | if (signature->verify != NULL) |
124 | 0 | break; |
125 | 0 | signature->verify = OSSL_FUNC_signature_verify(fns); |
126 | 0 | break; |
127 | 0 | case OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT: |
128 | 0 | if (signature->verify_message_init != NULL) |
129 | 0 | break; |
130 | 0 | signature->verify_message_init |
131 | 0 | = OSSL_FUNC_signature_verify_message_init(fns); |
132 | 0 | initfncnt++; |
133 | 0 | break; |
134 | 0 | case OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE: |
135 | 0 | if (signature->verify_message_update != NULL) |
136 | 0 | break; |
137 | 0 | signature->verify_message_update |
138 | 0 | = OSSL_FUNC_signature_verify_message_update(fns); |
139 | 0 | break; |
140 | 0 | case OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL: |
141 | 0 | if (signature->verify_message_final != NULL) |
142 | 0 | break; |
143 | 0 | signature->verify_message_final |
144 | 0 | = OSSL_FUNC_signature_verify_message_final(fns); |
145 | 0 | break; |
146 | 0 | case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT: |
147 | 0 | if (signature->verify_recover_init != NULL) |
148 | 0 | break; |
149 | 0 | signature->verify_recover_init |
150 | 0 | = OSSL_FUNC_signature_verify_recover_init(fns); |
151 | 0 | initfncnt++; |
152 | 0 | break; |
153 | 0 | case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER: |
154 | 0 | if (signature->verify_recover != NULL) |
155 | 0 | break; |
156 | 0 | signature->verify_recover |
157 | 0 | = OSSL_FUNC_signature_verify_recover(fns); |
158 | 0 | break; |
159 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT: |
160 | 0 | if (signature->digest_sign_init != NULL) |
161 | 0 | break; |
162 | 0 | signature->digest_sign_init |
163 | 0 | = OSSL_FUNC_signature_digest_sign_init(fns); |
164 | 0 | initfncnt++; |
165 | 0 | break; |
166 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE: |
167 | 0 | if (signature->digest_sign_update != NULL) |
168 | 0 | break; |
169 | 0 | signature->digest_sign_update |
170 | 0 | = OSSL_FUNC_signature_digest_sign_update(fns); |
171 | 0 | break; |
172 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL: |
173 | 0 | if (signature->digest_sign_final != NULL) |
174 | 0 | break; |
175 | 0 | signature->digest_sign_final |
176 | 0 | = OSSL_FUNC_signature_digest_sign_final(fns); |
177 | 0 | break; |
178 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_SIGN: |
179 | 0 | if (signature->digest_sign != NULL) |
180 | 0 | break; |
181 | 0 | signature->digest_sign |
182 | 0 | = OSSL_FUNC_signature_digest_sign(fns); |
183 | 0 | break; |
184 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT: |
185 | 0 | if (signature->digest_verify_init != NULL) |
186 | 0 | break; |
187 | 0 | signature->digest_verify_init |
188 | 0 | = OSSL_FUNC_signature_digest_verify_init(fns); |
189 | 0 | initfncnt++; |
190 | 0 | break; |
191 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE: |
192 | 0 | if (signature->digest_verify_update != NULL) |
193 | 0 | break; |
194 | 0 | signature->digest_verify_update |
195 | 0 | = OSSL_FUNC_signature_digest_verify_update(fns); |
196 | 0 | break; |
197 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL: |
198 | 0 | if (signature->digest_verify_final != NULL) |
199 | 0 | break; |
200 | 0 | signature->digest_verify_final |
201 | 0 | = OSSL_FUNC_signature_digest_verify_final(fns); |
202 | 0 | break; |
203 | 0 | case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY: |
204 | 0 | if (signature->digest_verify != NULL) |
205 | 0 | break; |
206 | 0 | signature->digest_verify |
207 | 0 | = OSSL_FUNC_signature_digest_verify(fns); |
208 | 0 | break; |
209 | 0 | case OSSL_FUNC_SIGNATURE_FREECTX: |
210 | 0 | if (signature->freectx != NULL) |
211 | 0 | break; |
212 | 0 | signature->freectx = OSSL_FUNC_signature_freectx(fns); |
213 | 0 | ctxfncnt++; |
214 | 0 | break; |
215 | 0 | case OSSL_FUNC_SIGNATURE_DUPCTX: |
216 | 0 | if (signature->dupctx != NULL) |
217 | 0 | break; |
218 | 0 | signature->dupctx = OSSL_FUNC_signature_dupctx(fns); |
219 | 0 | break; |
220 | 0 | case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS: |
221 | 0 | if (signature->get_ctx_params != NULL) |
222 | 0 | break; |
223 | 0 | signature->get_ctx_params |
224 | 0 | = OSSL_FUNC_signature_get_ctx_params(fns); |
225 | 0 | gparamfncnt++; |
226 | 0 | break; |
227 | 0 | case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS: |
228 | 0 | if (signature->gettable_ctx_params != NULL) |
229 | 0 | break; |
230 | 0 | signature->gettable_ctx_params |
231 | 0 | = OSSL_FUNC_signature_gettable_ctx_params(fns); |
232 | 0 | gparamfncnt++; |
233 | 0 | break; |
234 | 0 | case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS: |
235 | 0 | if (signature->set_ctx_params != NULL) |
236 | 0 | break; |
237 | 0 | signature->set_ctx_params |
238 | 0 | = OSSL_FUNC_signature_set_ctx_params(fns); |
239 | 0 | sparamfncnt++; |
240 | 0 | break; |
241 | 0 | case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS: |
242 | 0 | if (signature->settable_ctx_params != NULL) |
243 | 0 | break; |
244 | 0 | signature->settable_ctx_params |
245 | 0 | = OSSL_FUNC_signature_settable_ctx_params(fns); |
246 | 0 | sparamfncnt++; |
247 | 0 | break; |
248 | 0 | case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS: |
249 | 0 | if (signature->get_ctx_md_params != NULL) |
250 | 0 | break; |
251 | 0 | signature->get_ctx_md_params |
252 | 0 | = OSSL_FUNC_signature_get_ctx_md_params(fns); |
253 | 0 | gmdparamfncnt++; |
254 | 0 | break; |
255 | 0 | case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS: |
256 | 0 | if (signature->gettable_ctx_md_params != NULL) |
257 | 0 | break; |
258 | 0 | signature->gettable_ctx_md_params |
259 | 0 | = OSSL_FUNC_signature_gettable_ctx_md_params(fns); |
260 | 0 | gmdparamfncnt++; |
261 | 0 | break; |
262 | 0 | case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS: |
263 | 0 | if (signature->set_ctx_md_params != NULL) |
264 | 0 | break; |
265 | 0 | signature->set_ctx_md_params |
266 | 0 | = OSSL_FUNC_signature_set_ctx_md_params(fns); |
267 | 0 | smdparamfncnt++; |
268 | 0 | break; |
269 | 0 | case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS: |
270 | 0 | if (signature->settable_ctx_md_params != NULL) |
271 | 0 | break; |
272 | 0 | signature->settable_ctx_md_params |
273 | 0 | = OSSL_FUNC_signature_settable_ctx_md_params(fns); |
274 | 0 | smdparamfncnt++; |
275 | 0 | break; |
276 | 0 | case OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES: |
277 | 0 | if (signature->query_key_types != NULL) |
278 | 0 | break; |
279 | 0 | signature->query_key_types |
280 | 0 | = OSSL_FUNC_signature_query_key_types(fns); |
281 | 0 | break; |
282 | 0 | } |
283 | 0 | } |
284 | | /* |
285 | | * In order to be a consistent set of functions we must have at least |
286 | | * a set of context functions (newctx and freectx) as well as a set of |
287 | | * "signature" functions. Because there's an overlap between some sets |
288 | | * of functions, counters don't always cut it, we must test known |
289 | | * combinations. |
290 | | * We start by assuming the implementation is valid, and then look for |
291 | | * reasons it's not. |
292 | | */ |
293 | 0 | valid = 1; |
294 | | /* Start with the ones where counters say enough */ |
295 | 0 | if (ctxfncnt != 2) { |
296 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
297 | 0 | "missing %s newctx or freectx:%s", signature->type_name, desc); |
298 | 0 | valid = 0; |
299 | 0 | } |
300 | 0 | if (valid |
301 | 0 | && ((gparamfncnt != 0 && gparamfncnt != 2) |
302 | 0 | || (sparamfncnt != 0 && sparamfncnt != 2) |
303 | 0 | || (gmdparamfncnt != 0 && gmdparamfncnt != 2) |
304 | 0 | || (smdparamfncnt != 0 && smdparamfncnt != 2))) { |
305 | | /* |
306 | | * Params functions are optional, but if defined, they must |
307 | | * be pairwise complete sets, i.e. a getter must have an |
308 | | * associated gettable, etc |
309 | | */ |
310 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
311 | 0 | "missing %s params getter or setter:%s", signature->type_name, desc); |
312 | 0 | valid = 0; |
313 | 0 | } |
314 | 0 | if (valid && initfncnt == 0) { |
315 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
316 | 0 | "missing %s init:%s", signature->type_name, desc); |
317 | 0 | valid = 0; |
318 | 0 | } |
319 | | |
320 | | /* Now we check for function combinations */ |
321 | 0 | if (valid |
322 | 0 | && ((signature->sign_init != NULL |
323 | 0 | && signature->sign == NULL) |
324 | 0 | || (signature->sign_message_init != NULL |
325 | 0 | && signature->sign == NULL |
326 | 0 | && (signature->sign_message_update == NULL |
327 | 0 | || signature->sign_message_final == NULL)))) { |
328 | | /* sign_init function(s) with no signing function? That's weird */ |
329 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
330 | 0 | "missing %s signing function:%s", signature->type_name, desc); |
331 | 0 | valid = 0; |
332 | 0 | } |
333 | 0 | if (valid |
334 | 0 | && (signature->sign != NULL |
335 | 0 | || signature->sign_message_update != NULL |
336 | 0 | || signature->sign_message_final != NULL) |
337 | 0 | && signature->sign_init == NULL |
338 | 0 | && signature->sign_message_init == NULL) { |
339 | | /* signing function(s) with no sign_init? That's odd */ |
340 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
341 | 0 | "missing %s sign_init or sign_message_init:%s", signature->type_name, desc); |
342 | 0 | valid = 0; |
343 | 0 | } |
344 | |
|
345 | 0 | if (valid |
346 | 0 | && ((signature->verify_init != NULL |
347 | 0 | && signature->verify == NULL) |
348 | 0 | || (signature->verify_message_init != NULL |
349 | 0 | && signature->verify == NULL |
350 | 0 | && (signature->verify_message_update == NULL |
351 | 0 | || signature->verify_message_final == NULL)))) { |
352 | | /* verify_init function(s) with no verification function? That's weird */ |
353 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
354 | 0 | "missing %s verification function:%s", signature->type_name, desc); |
355 | 0 | valid = 0; |
356 | 0 | } |
357 | 0 | if (valid |
358 | 0 | && (signature->verify != NULL |
359 | 0 | || signature->verify_message_update != NULL |
360 | 0 | || signature->verify_message_final != NULL) |
361 | 0 | && signature->verify_init == NULL |
362 | 0 | && signature->verify_message_init == NULL) { |
363 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
364 | 0 | "missing %s verify_init or verify_message_init:%s", |
365 | 0 | signature->type_name, desc); |
366 | | /* verification function(s) with no verify_init? That's odd */ |
367 | 0 | valid = 0; |
368 | 0 | } |
369 | |
|
370 | 0 | if (valid |
371 | 0 | && (signature->verify_recover_init != NULL) |
372 | 0 | && (signature->verify_recover == NULL)) { |
373 | | /* verify_recover_init function with no verify_recover? How quaint */ |
374 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
375 | 0 | "missing %s verify_recover:%s", signature->type_name, desc); |
376 | 0 | valid = 0; |
377 | 0 | } |
378 | |
|
379 | 0 | if (valid |
380 | 0 | && (signature->digest_sign_init != NULL |
381 | 0 | && signature->digest_sign == NULL |
382 | 0 | && (signature->digest_sign_update == NULL |
383 | 0 | || signature->digest_sign_final == NULL))) { |
384 | | /* You can't have a digest_sign_init without *some* performing functions */ |
385 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
386 | 0 | "missing %s digest_sign function:%s", signature->type_name, desc); |
387 | 0 | valid = 0; |
388 | 0 | } |
389 | |
|
390 | 0 | if (valid |
391 | 0 | && ((signature->digest_verify_init != NULL |
392 | 0 | && signature->digest_verify == NULL |
393 | 0 | && (signature->digest_verify_update == NULL |
394 | 0 | || signature->digest_verify_final == NULL)))) { |
395 | | /* You can't have a digest_verify_init without *some* performing functions */ |
396 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
397 | 0 | "missing %s digest_verify function:%s", signature->type_name, desc); |
398 | 0 | valid = 0; |
399 | 0 | } |
400 | |
|
401 | 0 | if (!valid) |
402 | 0 | goto err; |
403 | | |
404 | 0 | if ((signature->digest_sign != NULL |
405 | 0 | || signature->digest_sign_update != NULL |
406 | 0 | || signature->digest_sign_final != NULL) |
407 | 0 | && signature->digest_sign_init == NULL) { |
408 | | /* digest signing function(s) with no digest_sign_init? That's odd */ |
409 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
410 | 0 | "missing %s digest_sign_init:%s", signature->type_name, desc); |
411 | 0 | goto err; |
412 | 0 | } |
413 | | |
414 | 0 | if ((signature->digest_verify != NULL |
415 | 0 | || signature->digest_verify_update != NULL |
416 | 0 | || signature->digest_verify_final != NULL) |
417 | 0 | && signature->digest_verify_init == NULL) { |
418 | | /* digest verification function(s) with no digest_verify_init? That's odd */ |
419 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
420 | 0 | "missing %s digest_verify_init:%s", signature->type_name, desc); |
421 | 0 | goto err; |
422 | 0 | } |
423 | | |
424 | 0 | if ((signature->sign_message_update == NULL) != |
425 | 0 | (signature->sign_message_final == NULL)) { |
426 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
427 | 0 | "only one of %s message signing update and final available:%s", |
428 | 0 | signature->type_name, desc); |
429 | 0 | goto err; |
430 | 0 | } |
431 | 0 | if ((signature->verify_message_update == NULL) != |
432 | 0 | (signature->verify_message_final == NULL)) { |
433 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
434 | 0 | "only one of %s message verification update and final available:%s", |
435 | 0 | signature->type_name, desc); |
436 | 0 | goto err; |
437 | 0 | } |
438 | 0 | if ((signature->digest_sign_update == NULL) != |
439 | 0 | (signature->digest_sign_final == NULL)) { |
440 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
441 | 0 | "only one of %s digest signing update and final available:%s", |
442 | 0 | signature->type_name, desc); |
443 | 0 | goto err; |
444 | 0 | } |
445 | 0 | if ((signature->digest_verify_update == NULL) != |
446 | 0 | (signature->digest_verify_final == NULL)) { |
447 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS, |
448 | 0 | "only one of %s digest verification update and final available:%s", |
449 | 0 | signature->type_name, desc); |
450 | 0 | goto err; |
451 | 0 | } |
452 | | |
453 | 0 | return signature; |
454 | 0 | err: |
455 | 0 | EVP_SIGNATURE_free(signature); |
456 | 0 | return NULL; |
457 | 0 | } |
458 | | |
459 | | void EVP_SIGNATURE_free(EVP_SIGNATURE *signature) |
460 | 0 | { |
461 | 0 | int i; |
462 | |
|
463 | 0 | if (signature == NULL) |
464 | 0 | return; |
465 | 0 | CRYPTO_DOWN_REF(&signature->refcnt, &i); |
466 | 0 | if (i > 0) |
467 | 0 | return; |
468 | 0 | OPENSSL_free(signature->type_name); |
469 | 0 | ossl_provider_free(signature->prov); |
470 | 0 | CRYPTO_FREE_REF(&signature->refcnt); |
471 | 0 | OPENSSL_free(signature); |
472 | 0 | } |
473 | | |
474 | | int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature) |
475 | 0 | { |
476 | 0 | int ref = 0; |
477 | |
|
478 | 0 | CRYPTO_UP_REF(&signature->refcnt, &ref); |
479 | 0 | return 1; |
480 | 0 | } |
481 | | |
482 | | OSSL_PROVIDER *EVP_SIGNATURE_get0_provider(const EVP_SIGNATURE *signature) |
483 | 0 | { |
484 | 0 | return signature->prov; |
485 | 0 | } |
486 | | |
487 | | EVP_SIGNATURE *EVP_SIGNATURE_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
488 | | const char *properties) |
489 | 0 | { |
490 | 0 | return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties, |
491 | 0 | evp_signature_from_algorithm, |
492 | 0 | evp_signature_up_ref, |
493 | 0 | evp_signature_free); |
494 | 0 | } |
495 | | |
496 | | EVP_SIGNATURE *evp_signature_fetch_from_prov(OSSL_PROVIDER *prov, |
497 | | const char *algorithm, |
498 | | const char *properties) |
499 | 0 | { |
500 | 0 | return evp_generic_fetch_from_prov(prov, OSSL_OP_SIGNATURE, |
501 | 0 | algorithm, properties, |
502 | 0 | evp_signature_from_algorithm, |
503 | 0 | evp_signature_up_ref, |
504 | 0 | evp_signature_free); |
505 | 0 | } |
506 | | |
507 | | int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name) |
508 | 0 | { |
509 | 0 | return signature != NULL |
510 | 0 | && evp_is_a(signature->prov, signature->name_id, NULL, name); |
511 | 0 | } |
512 | | |
513 | | int evp_signature_get_number(const EVP_SIGNATURE *signature) |
514 | 0 | { |
515 | 0 | return signature->name_id; |
516 | 0 | } |
517 | | |
518 | | const char *EVP_SIGNATURE_get0_name(const EVP_SIGNATURE *signature) |
519 | 0 | { |
520 | 0 | return signature->type_name; |
521 | 0 | } |
522 | | |
523 | | const char *EVP_SIGNATURE_get0_description(const EVP_SIGNATURE *signature) |
524 | 0 | { |
525 | 0 | return signature->description; |
526 | 0 | } |
527 | | |
528 | | void EVP_SIGNATURE_do_all_provided(OSSL_LIB_CTX *libctx, |
529 | | void (*fn)(EVP_SIGNATURE *signature, |
530 | | void *arg), |
531 | | void *arg) |
532 | 0 | { |
533 | 0 | evp_generic_do_all(libctx, OSSL_OP_SIGNATURE, |
534 | 0 | (void (*)(void *, void *))fn, arg, |
535 | 0 | evp_signature_from_algorithm, |
536 | 0 | evp_signature_up_ref, |
537 | 0 | evp_signature_free); |
538 | 0 | } |
539 | | |
540 | | |
541 | | int EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature, |
542 | | void (*fn)(const char *name, void *data), |
543 | | void *data) |
544 | 0 | { |
545 | 0 | if (signature->prov != NULL) |
546 | 0 | return evp_names_do_all(signature->prov, signature->name_id, fn, data); |
547 | | |
548 | 0 | return 1; |
549 | 0 | } |
550 | | |
551 | | const OSSL_PARAM *EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig) |
552 | 0 | { |
553 | 0 | void *provctx; |
554 | |
|
555 | 0 | if (sig == NULL || sig->gettable_ctx_params == NULL) |
556 | 0 | return NULL; |
557 | | |
558 | 0 | provctx = ossl_provider_ctx(EVP_SIGNATURE_get0_provider(sig)); |
559 | 0 | return sig->gettable_ctx_params(NULL, provctx); |
560 | 0 | } |
561 | | |
562 | | const OSSL_PARAM *EVP_SIGNATURE_settable_ctx_params(const EVP_SIGNATURE *sig) |
563 | 0 | { |
564 | 0 | void *provctx; |
565 | |
|
566 | 0 | if (sig == NULL || sig->settable_ctx_params == NULL) |
567 | 0 | return NULL; |
568 | | |
569 | 0 | provctx = ossl_provider_ctx(EVP_SIGNATURE_get0_provider(sig)); |
570 | 0 | return sig->settable_ctx_params(NULL, provctx); |
571 | 0 | } |
572 | | |
573 | | static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature, |
574 | | int operation, const OSSL_PARAM params[]) |
575 | 0 | { |
576 | 0 | const char *desc; |
577 | 0 | int ret = 0; |
578 | 0 | void *provkey = NULL; |
579 | 0 | EVP_KEYMGMT *tmp_keymgmt = NULL; |
580 | 0 | const OSSL_PROVIDER *tmp_prov = NULL; |
581 | 0 | const char *supported_sig = NULL; |
582 | 0 | int iter; |
583 | |
|
584 | 0 | if (ctx == NULL) { |
585 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
586 | 0 | return -1; |
587 | 0 | } |
588 | | |
589 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
590 | 0 | ctx->operation = operation; |
591 | |
|
592 | 0 | if (signature != NULL) { |
593 | | /* |
594 | | * It's important to figure out what the key type should be, and if |
595 | | * that is what we have in ctx. |
596 | | */ |
597 | |
|
598 | 0 | EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; |
599 | |
|
600 | 0 | if (ctx->pkey == NULL) { |
601 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
602 | 0 | goto err; |
603 | 0 | } |
604 | | |
605 | | /* |
606 | | * Ensure that the key is provided, either natively, or as a |
607 | | * cached export. We start by fetching the keymgmt with the same |
608 | | * name as |ctx->pkey|, but from the provider of the signature |
609 | | * method, using the same property query as when fetching the |
610 | | * signature method. With the keymgmt we found (if we did), we |
611 | | * try to export |ctx->pkey| to it (evp_pkey_export_to_provider() |
612 | | * is smart enough to only actually export it if |tmp_keymgmt| |
613 | | * is different from |ctx->pkey|'s keymgmt) |
614 | | */ |
615 | 0 | tmp_prov = EVP_SIGNATURE_get0_provider(signature); |
616 | 0 | tmp_keymgmt_tofree = tmp_keymgmt = |
617 | 0 | evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
618 | 0 | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
619 | 0 | ctx->propquery); |
620 | 0 | if (tmp_keymgmt != NULL) |
621 | 0 | provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
622 | 0 | &tmp_keymgmt, ctx->propquery); |
623 | 0 | if (tmp_keymgmt == NULL) |
624 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
625 | |
|
626 | 0 | if (provkey == NULL) |
627 | 0 | goto end; |
628 | | |
629 | | /* |
630 | | * Check that the signature matches the given key. This is not |
631 | | * designed to work with legacy keys, so has to be done after we've |
632 | | * ensured that the key is at least exported to a provider (above). |
633 | | */ |
634 | 0 | if (signature->query_key_types != NULL) { |
635 | | /* This is expected to be a NULL-terminated array */ |
636 | 0 | const char **keytypes; |
637 | |
|
638 | 0 | keytypes = signature->query_key_types(); |
639 | 0 | for (; *keytypes != NULL; keytypes++) |
640 | 0 | if (EVP_PKEY_CTX_is_a(ctx, *keytypes)) |
641 | 0 | break; |
642 | 0 | if (*keytypes == NULL) { |
643 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_SIGNATURE_TYPE_AND_KEY_TYPE_INCOMPATIBLE); |
644 | 0 | return -2; |
645 | 0 | } |
646 | 0 | } else { |
647 | | /* |
648 | | * Fallback 1: |
649 | | * check if the keytype is the same as the signature algorithm name |
650 | | */ |
651 | 0 | const char *keytype = EVP_KEYMGMT_get0_name(ctx->keymgmt); |
652 | 0 | int ok = EVP_SIGNATURE_is_a(signature, keytype); |
653 | | |
654 | | /* |
655 | | * Fallback 2: |
656 | | * query the pkey for a default signature algorithm name, and check |
657 | | * if it matches the signature implementation |
658 | | */ |
659 | 0 | if (!ok) { |
660 | 0 | const char *signame |
661 | 0 | = evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
662 | 0 | OSSL_OP_SIGNATURE); |
663 | |
|
664 | 0 | ok = EVP_SIGNATURE_is_a(signature, signame); |
665 | 0 | } |
666 | | |
667 | | /* If none of the fallbacks helped, we're lost */ |
668 | 0 | if (!ok) { |
669 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_SIGNATURE_TYPE_AND_KEY_TYPE_INCOMPATIBLE); |
670 | 0 | return -2; |
671 | 0 | } |
672 | 0 | } |
673 | | |
674 | 0 | if (!EVP_SIGNATURE_up_ref(signature)) |
675 | 0 | return 0; |
676 | 0 | } else { |
677 | | /* Without a pre-fetched signature, it must be figured out somehow */ |
678 | 0 | ERR_set_mark(); |
679 | |
|
680 | 0 | if (evp_pkey_ctx_is_legacy(ctx)) |
681 | 0 | goto legacy; |
682 | | |
683 | 0 | if (ctx->pkey == NULL) { |
684 | 0 | ERR_clear_last_mark(); |
685 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
686 | 0 | goto err; |
687 | 0 | } |
688 | | |
689 | | /* |
690 | | * Try to derive the supported signature from |ctx->keymgmt|. |
691 | | */ |
692 | 0 | if (!ossl_assert(ctx->pkey->keymgmt == NULL |
693 | 0 | || ctx->pkey->keymgmt == ctx->keymgmt)) { |
694 | 0 | ERR_clear_last_mark(); |
695 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
696 | 0 | goto err; |
697 | 0 | } |
698 | 0 | supported_sig |
699 | 0 | = evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
700 | 0 | OSSL_OP_SIGNATURE); |
701 | 0 | if (supported_sig == NULL) { |
702 | 0 | ERR_clear_last_mark(); |
703 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
704 | 0 | goto err; |
705 | 0 | } |
706 | | |
707 | | /* |
708 | | * We perform two iterations: |
709 | | * |
710 | | * 1. Do the normal signature fetch, using the fetching data given by |
711 | | * the EVP_PKEY_CTX. |
712 | | * 2. Do the provider specific signature fetch, from the same provider |
713 | | * as |ctx->keymgmt| |
714 | | * |
715 | | * We then try to fetch the keymgmt from the same provider as the |
716 | | * signature, and try to export |ctx->pkey| to that keymgmt (when |
717 | | * this keymgmt happens to be the same as |ctx->keymgmt|, the export |
718 | | * is a no-op, but we call it anyway to not complicate the code even |
719 | | * more). |
720 | | * If the export call succeeds (returns a non-NULL provider key pointer), |
721 | | * we're done and can perform the operation itself. If not, we perform |
722 | | * the second iteration, or jump to legacy. |
723 | | */ |
724 | 0 | for (iter = 1; iter < 3 && provkey == NULL; iter++) { |
725 | 0 | EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; |
726 | | |
727 | | /* |
728 | | * If we're on the second iteration, free the results from the first. |
729 | | * They are NULL on the first iteration, so no need to check what |
730 | | * iteration we're on. |
731 | | */ |
732 | 0 | EVP_SIGNATURE_free(signature); |
733 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
734 | |
|
735 | 0 | switch (iter) { |
736 | 0 | case 1: |
737 | 0 | signature = |
738 | 0 | EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery); |
739 | 0 | if (signature != NULL) |
740 | 0 | tmp_prov = EVP_SIGNATURE_get0_provider(signature); |
741 | 0 | break; |
742 | 0 | case 2: |
743 | 0 | tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
744 | 0 | signature = |
745 | 0 | evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
746 | 0 | supported_sig, ctx->propquery); |
747 | 0 | if (signature == NULL) |
748 | 0 | goto legacy; |
749 | 0 | break; |
750 | 0 | } |
751 | 0 | if (signature == NULL) |
752 | 0 | continue; |
753 | | |
754 | | /* |
755 | | * Ensure that the key is provided, either natively, or as a |
756 | | * cached export. We start by fetching the keymgmt with the same |
757 | | * name as |ctx->pkey|, but from the provider of the signature |
758 | | * method, using the same property query as when fetching the |
759 | | * signature method. With the keymgmt we found (if we did), we |
760 | | * try to export |ctx->pkey| to it (evp_pkey_export_to_provider() |
761 | | * is smart enough to only actually export it if |tmp_keymgmt| |
762 | | * is different from |ctx->pkey|'s keymgmt) |
763 | | */ |
764 | 0 | tmp_keymgmt_tofree = tmp_keymgmt = |
765 | 0 | evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
766 | 0 | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
767 | 0 | ctx->propquery); |
768 | 0 | if (tmp_keymgmt != NULL) |
769 | 0 | provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
770 | 0 | &tmp_keymgmt, ctx->propquery); |
771 | 0 | if (tmp_keymgmt == NULL) |
772 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
773 | 0 | } |
774 | | |
775 | 0 | if (provkey == NULL) { |
776 | 0 | EVP_SIGNATURE_free(signature); |
777 | 0 | goto legacy; |
778 | 0 | } |
779 | | |
780 | 0 | ERR_pop_to_mark(); |
781 | 0 | } |
782 | | |
783 | | /* No more legacy from here down to legacy: */ |
784 | | |
785 | 0 | ctx->op.sig.signature = signature; |
786 | 0 | desc = signature->description != NULL ? signature->description : ""; |
787 | |
|
788 | 0 | ctx->op.sig.algctx = |
789 | 0 | signature->newctx(ossl_provider_ctx(signature->prov), ctx->propquery); |
790 | 0 | if (ctx->op.sig.algctx == NULL) { |
791 | | /* The provider key can stay in the cache */ |
792 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
793 | 0 | goto err; |
794 | 0 | } |
795 | | |
796 | 0 | switch (operation) { |
797 | 0 | case EVP_PKEY_OP_SIGN: |
798 | 0 | if (signature->sign_init == NULL) { |
799 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
800 | 0 | "%s sign_init:%s", signature->type_name, desc); |
801 | 0 | ret = -2; |
802 | 0 | goto err; |
803 | 0 | } |
804 | 0 | ret = signature->sign_init(ctx->op.sig.algctx, provkey, params); |
805 | 0 | break; |
806 | 0 | case EVP_PKEY_OP_SIGNMSG: |
807 | 0 | if (signature->sign_message_init == NULL) { |
808 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
809 | 0 | "%s sign_message_init:%s", signature->type_name, desc); |
810 | 0 | ret = -2; |
811 | 0 | goto err; |
812 | 0 | } |
813 | 0 | ret = signature->sign_message_init(ctx->op.sig.algctx, provkey, params); |
814 | 0 | break; |
815 | 0 | case EVP_PKEY_OP_VERIFY: |
816 | 0 | if (signature->verify_init == NULL) { |
817 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
818 | 0 | "%s verify_init:%s", signature->type_name, desc); |
819 | 0 | ret = -2; |
820 | 0 | goto err; |
821 | 0 | } |
822 | 0 | ret = signature->verify_init(ctx->op.sig.algctx, provkey, params); |
823 | 0 | break; |
824 | 0 | case EVP_PKEY_OP_VERIFYMSG: |
825 | 0 | if (signature->verify_message_init == NULL) { |
826 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
827 | 0 | "%s verify_message_init:%s", signature->type_name, desc); |
828 | 0 | ret = -2; |
829 | 0 | goto err; |
830 | 0 | } |
831 | 0 | ret = signature->verify_message_init(ctx->op.sig.algctx, provkey, params); |
832 | 0 | break; |
833 | 0 | case EVP_PKEY_OP_VERIFYRECOVER: |
834 | 0 | if (signature->verify_recover_init == NULL) { |
835 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
836 | 0 | "%s verify_recover_init:%s", signature->type_name, desc); |
837 | 0 | ret = -2; |
838 | 0 | goto err; |
839 | 0 | } |
840 | 0 | ret = signature->verify_recover_init(ctx->op.sig.algctx, provkey, params); |
841 | 0 | break; |
842 | 0 | default: |
843 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
844 | 0 | goto err; |
845 | 0 | } |
846 | | |
847 | 0 | if (ret <= 0) { |
848 | 0 | signature->freectx(ctx->op.sig.algctx); |
849 | 0 | ctx->op.sig.algctx = NULL; |
850 | 0 | goto err; |
851 | 0 | } |
852 | 0 | goto end; |
853 | | |
854 | 0 | legacy: |
855 | | /* |
856 | | * If we don't have the full support we need with provided methods, |
857 | | * let's go see if legacy does. |
858 | | */ |
859 | 0 | ERR_pop_to_mark(); |
860 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
861 | 0 | tmp_keymgmt = NULL; |
862 | |
|
863 | 0 | if (ctx->pmeth == NULL |
864 | 0 | || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL) |
865 | 0 | || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL) |
866 | 0 | || (operation == EVP_PKEY_OP_VERIFYRECOVER |
867 | 0 | && ctx->pmeth->verify_recover == NULL)) { |
868 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
869 | 0 | return -2; |
870 | 0 | } |
871 | | |
872 | 0 | switch (operation) { |
873 | 0 | case EVP_PKEY_OP_SIGN: |
874 | 0 | if (ctx->pmeth->sign_init == NULL) |
875 | 0 | return 1; |
876 | 0 | ret = ctx->pmeth->sign_init(ctx); |
877 | 0 | break; |
878 | 0 | case EVP_PKEY_OP_VERIFY: |
879 | 0 | if (ctx->pmeth->verify_init == NULL) |
880 | 0 | return 1; |
881 | 0 | ret = ctx->pmeth->verify_init(ctx); |
882 | 0 | break; |
883 | 0 | case EVP_PKEY_OP_VERIFYRECOVER: |
884 | 0 | if (ctx->pmeth->verify_recover_init == NULL) |
885 | 0 | return 1; |
886 | 0 | ret = ctx->pmeth->verify_recover_init(ctx); |
887 | 0 | break; |
888 | 0 | default: |
889 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
890 | 0 | goto err; |
891 | 0 | } |
892 | 0 | if (ret <= 0) |
893 | 0 | goto err; |
894 | 0 | end: |
895 | 0 | #ifndef FIPS_MODULE |
896 | 0 | if (ret > 0) |
897 | 0 | ret = evp_pkey_ctx_use_cached_data(ctx); |
898 | 0 | #endif |
899 | |
|
900 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
901 | 0 | return ret; |
902 | 0 | err: |
903 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
904 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
905 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
906 | 0 | return ret; |
907 | 0 | } |
908 | | |
909 | | int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) |
910 | 0 | { |
911 | 0 | return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN, NULL); |
912 | 0 | } |
913 | | |
914 | | int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
915 | 0 | { |
916 | 0 | return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN, params); |
917 | 0 | } |
918 | | |
919 | | int EVP_PKEY_sign_init_ex2(EVP_PKEY_CTX *ctx, |
920 | | EVP_SIGNATURE *algo, const OSSL_PARAM params[]) |
921 | 0 | { |
922 | 0 | return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_SIGN, params); |
923 | 0 | } |
924 | | |
925 | | int EVP_PKEY_sign_message_init(EVP_PKEY_CTX *ctx, |
926 | | EVP_SIGNATURE *algo, const OSSL_PARAM params[]) |
927 | 0 | { |
928 | 0 | return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_SIGNMSG, params); |
929 | 0 | } |
930 | | |
931 | | int EVP_PKEY_sign_message_update(EVP_PKEY_CTX *ctx, |
932 | | const unsigned char *in, size_t inlen) |
933 | 0 | { |
934 | 0 | EVP_SIGNATURE *signature; |
935 | 0 | const char *desc; |
936 | 0 | int ret; |
937 | |
|
938 | 0 | if (ctx == NULL) { |
939 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
940 | 0 | return -1; |
941 | 0 | } |
942 | | |
943 | 0 | if (ctx->operation != EVP_PKEY_OP_SIGNMSG) { |
944 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
945 | 0 | return -1; |
946 | 0 | } |
947 | | |
948 | 0 | signature = ctx->op.sig.signature; |
949 | 0 | desc = signature->description != NULL ? signature->description : ""; |
950 | 0 | if (signature->sign_message_update == NULL) { |
951 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
952 | 0 | "%s sign_message_update:%s", signature->type_name, desc); |
953 | 0 | return -2; |
954 | 0 | } |
955 | | |
956 | 0 | ret = signature->sign_message_update(ctx->op.sig.algctx, in, inlen); |
957 | 0 | if (ret <= 0) |
958 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
959 | 0 | "%s sign_message_update:%s", signature->type_name, desc); |
960 | 0 | return ret; |
961 | 0 | } |
962 | | |
963 | | int EVP_PKEY_sign_message_final(EVP_PKEY_CTX *ctx, |
964 | | unsigned char *sig, size_t *siglen) |
965 | 0 | { |
966 | 0 | EVP_SIGNATURE *signature; |
967 | 0 | const char *desc; |
968 | 0 | int ret; |
969 | |
|
970 | 0 | if (ctx == NULL) { |
971 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
972 | 0 | return -1; |
973 | 0 | } |
974 | | |
975 | 0 | if (ctx->operation != EVP_PKEY_OP_SIGNMSG) { |
976 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
977 | 0 | return -1; |
978 | 0 | } |
979 | | |
980 | 0 | signature = ctx->op.sig.signature; |
981 | 0 | desc = signature->description != NULL ? signature->description : ""; |
982 | 0 | if (signature->sign_message_final == NULL) { |
983 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
984 | 0 | "%s sign_message_final:%s", signature->type_name, desc); |
985 | 0 | return -2; |
986 | 0 | } |
987 | | |
988 | 0 | ret = signature->sign_message_final(ctx->op.sig.algctx, sig, siglen, |
989 | 0 | (sig == NULL) ? 0 : *siglen); |
990 | 0 | if (ret <= 0) |
991 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
992 | 0 | "%s sign_message_final:%s", signature->type_name, desc); |
993 | 0 | return ret; |
994 | 0 | } |
995 | | |
996 | | int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, |
997 | | unsigned char *sig, size_t *siglen, |
998 | | const unsigned char *tbs, size_t tbslen) |
999 | 0 | { |
1000 | 0 | EVP_SIGNATURE *signature; |
1001 | 0 | const char *desc; |
1002 | 0 | int ret; |
1003 | |
|
1004 | 0 | if (ctx == NULL) { |
1005 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1006 | 0 | return -1; |
1007 | 0 | } |
1008 | | |
1009 | 0 | if (ctx->operation != EVP_PKEY_OP_SIGN |
1010 | 0 | && ctx->operation != EVP_PKEY_OP_SIGNMSG) { |
1011 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
1012 | 0 | return -1; |
1013 | 0 | } |
1014 | | |
1015 | 0 | if (ctx->op.sig.algctx == NULL) |
1016 | 0 | goto legacy; |
1017 | | |
1018 | 0 | signature = ctx->op.sig.signature; |
1019 | 0 | desc = signature->description != NULL ? signature->description : ""; |
1020 | 0 | if (signature->sign == NULL) { |
1021 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
1022 | 0 | "%s sign:%s", signature->type_name, desc); |
1023 | 0 | return -2; |
1024 | 0 | } |
1025 | | |
1026 | 0 | ret = signature->sign(ctx->op.sig.algctx, sig, siglen, |
1027 | 0 | (sig == NULL) ? 0 : *siglen, tbs, tbslen); |
1028 | 0 | if (ret <= 0) |
1029 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
1030 | 0 | "%s sign:%s", signature->type_name, desc); |
1031 | 0 | return ret; |
1032 | 0 | legacy: |
1033 | |
|
1034 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) { |
1035 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
1036 | 0 | return -2; |
1037 | 0 | } |
1038 | | |
1039 | 0 | M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN) |
1040 | 0 | return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen); |
1041 | 0 | } |
1042 | | |
1043 | | int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) |
1044 | 0 | { |
1045 | 0 | return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY, NULL); |
1046 | 0 | } |
1047 | | |
1048 | | int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
1049 | 0 | { |
1050 | 0 | return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY, params); |
1051 | 0 | } |
1052 | | |
1053 | | int EVP_PKEY_verify_init_ex2(EVP_PKEY_CTX *ctx, |
1054 | | EVP_SIGNATURE *algo, const OSSL_PARAM params[]) |
1055 | 0 | { |
1056 | 0 | return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFY, params); |
1057 | 0 | } |
1058 | | |
1059 | | int EVP_PKEY_verify_message_init(EVP_PKEY_CTX *ctx, |
1060 | | EVP_SIGNATURE *algo, const OSSL_PARAM params[]) |
1061 | 0 | { |
1062 | 0 | return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFYMSG, params); |
1063 | 0 | } |
1064 | | |
1065 | | int EVP_PKEY_CTX_set_signature(EVP_PKEY_CTX *ctx, |
1066 | | const unsigned char *sig, size_t siglen) |
1067 | 0 | { |
1068 | 0 | OSSL_PARAM sig_params[2], *p = sig_params; |
1069 | |
|
1070 | 0 | if (ctx == NULL) { |
1071 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1072 | 0 | return 0; |
1073 | 0 | } |
1074 | | |
1075 | 0 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, |
1076 | | /* |
1077 | | * Cast away the const. This is |
1078 | | * read only so should be safe |
1079 | | */ |
1080 | 0 | (char *)sig, siglen); |
1081 | 0 | *p = OSSL_PARAM_construct_end(); |
1082 | |
|
1083 | 0 | return EVP_PKEY_CTX_set_params(ctx, sig_params); |
1084 | 0 | } |
1085 | | |
1086 | | int EVP_PKEY_verify_message_update(EVP_PKEY_CTX *ctx, |
1087 | | const unsigned char *in, size_t inlen) |
1088 | 0 | { |
1089 | 0 | EVP_SIGNATURE *signature; |
1090 | 0 | const char *desc; |
1091 | 0 | int ret; |
1092 | |
|
1093 | 0 | if (ctx == NULL) { |
1094 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1095 | 0 | return -1; |
1096 | 0 | } |
1097 | | |
1098 | 0 | if (ctx->operation != EVP_PKEY_OP_VERIFYMSG) { |
1099 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
1100 | 0 | return -1; |
1101 | 0 | } |
1102 | | |
1103 | 0 | signature = ctx->op.sig.signature; |
1104 | 0 | desc = signature->description != NULL ? signature->description : ""; |
1105 | 0 | if (signature->verify_message_update == NULL) { |
1106 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
1107 | 0 | "%s verify_message_update:%s", signature->type_name, desc); |
1108 | 0 | return -2; |
1109 | 0 | } |
1110 | | |
1111 | 0 | ret = signature->verify_message_update(ctx->op.sig.algctx, in, inlen); |
1112 | 0 | if (ret <= 0) |
1113 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
1114 | 0 | "%s verify_message_update:%s", signature->type_name, desc); |
1115 | 0 | return ret; |
1116 | 0 | } |
1117 | | |
1118 | | int EVP_PKEY_verify_message_final(EVP_PKEY_CTX *ctx) |
1119 | 0 | { |
1120 | 0 | EVP_SIGNATURE *signature; |
1121 | 0 | const char *desc; |
1122 | 0 | int ret; |
1123 | |
|
1124 | 0 | if (ctx == NULL) { |
1125 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1126 | 0 | return -1; |
1127 | 0 | } |
1128 | | |
1129 | 0 | if (ctx->operation != EVP_PKEY_OP_VERIFYMSG) { |
1130 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
1131 | 0 | return -1; |
1132 | 0 | } |
1133 | | |
1134 | 0 | signature = ctx->op.sig.signature; |
1135 | 0 | desc = signature->description != NULL ? signature->description : ""; |
1136 | 0 | if (signature->verify_message_final == NULL) { |
1137 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
1138 | 0 | "%s verify_message_final:%s", signature->type_name, desc); |
1139 | 0 | return -2; |
1140 | 0 | } |
1141 | | |
1142 | | /* The signature must have been set with EVP_PKEY_CTX_set_signature() */ |
1143 | 0 | ret = signature->verify_message_final(ctx->op.sig.algctx); |
1144 | 0 | if (ret <= 0) |
1145 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
1146 | 0 | "%s verify_message_final:%s", signature->type_name, desc); |
1147 | 0 | return ret; |
1148 | 0 | } |
1149 | | |
1150 | | int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, |
1151 | | const unsigned char *sig, size_t siglen, |
1152 | | const unsigned char *tbs, size_t tbslen) |
1153 | 0 | { |
1154 | 0 | EVP_SIGNATURE *signature; |
1155 | 0 | const char *desc; |
1156 | 0 | int ret; |
1157 | |
|
1158 | 0 | if (ctx == NULL) { |
1159 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1160 | 0 | return -1; |
1161 | 0 | } |
1162 | | |
1163 | 0 | if (ctx->operation != EVP_PKEY_OP_VERIFY |
1164 | 0 | && ctx->operation != EVP_PKEY_OP_VERIFYMSG) { |
1165 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
1166 | 0 | return -1; |
1167 | 0 | } |
1168 | | |
1169 | 0 | if (ctx->op.sig.algctx == NULL) |
1170 | 0 | goto legacy; |
1171 | | |
1172 | 0 | signature = ctx->op.sig.signature; |
1173 | 0 | desc = signature->description != NULL ? signature->description : ""; |
1174 | 0 | if (signature->verify == NULL) { |
1175 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
1176 | 0 | "%s verify:%s", signature->type_name, desc); |
1177 | 0 | return -2; |
1178 | 0 | } |
1179 | | |
1180 | 0 | ret = ctx->op.sig.signature->verify(ctx->op.sig.algctx, sig, siglen, |
1181 | 0 | tbs, tbslen); |
1182 | 0 | if (ret <= 0) |
1183 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
1184 | 0 | "%s verify:%s", signature->type_name, desc); |
1185 | |
|
1186 | 0 | return ret; |
1187 | 0 | legacy: |
1188 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) { |
1189 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
1190 | 0 | return -2; |
1191 | 0 | } |
1192 | | |
1193 | 0 | return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen); |
1194 | 0 | } |
1195 | | |
1196 | | int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) |
1197 | 0 | { |
1198 | 0 | return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER, NULL); |
1199 | 0 | } |
1200 | | |
1201 | | int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx, |
1202 | | const OSSL_PARAM params[]) |
1203 | 0 | { |
1204 | 0 | return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER, params); |
1205 | 0 | } |
1206 | | |
1207 | | int EVP_PKEY_verify_recover_init_ex2(EVP_PKEY_CTX *ctx, |
1208 | | EVP_SIGNATURE *algo, const OSSL_PARAM params[]) |
1209 | 0 | { |
1210 | 0 | return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFYRECOVER, params); |
1211 | 0 | } |
1212 | | |
1213 | | int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, |
1214 | | unsigned char *rout, size_t *routlen, |
1215 | | const unsigned char *sig, size_t siglen) |
1216 | 0 | { |
1217 | 0 | EVP_SIGNATURE *signature; |
1218 | 0 | const char *desc; |
1219 | 0 | int ret; |
1220 | |
|
1221 | 0 | if (ctx == NULL) { |
1222 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1223 | 0 | return -1; |
1224 | 0 | } |
1225 | | |
1226 | 0 | if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) { |
1227 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
1228 | 0 | return -1; |
1229 | 0 | } |
1230 | | |
1231 | 0 | if (ctx->op.sig.algctx == NULL) |
1232 | 0 | goto legacy; |
1233 | | |
1234 | 0 | signature = ctx->op.sig.signature; |
1235 | 0 | desc = signature->description != NULL ? signature->description : ""; |
1236 | 0 | if (signature->verify_recover == NULL) { |
1237 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
1238 | 0 | "%s verify_recover:%s", signature->type_name, desc); |
1239 | 0 | return -2; |
1240 | 0 | } |
1241 | | |
1242 | 0 | ret = signature->verify_recover(ctx->op.sig.algctx, rout, routlen, |
1243 | 0 | (rout == NULL ? 0 : *routlen), sig, siglen); |
1244 | 0 | if (ret <= 0) |
1245 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
1246 | 0 | "%s verify_recover:%s", signature->type_name, desc); |
1247 | 0 | return ret; |
1248 | 0 | legacy: |
1249 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) { |
1250 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
1251 | 0 | return -2; |
1252 | 0 | } |
1253 | 0 | M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER) |
1254 | 0 | return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen); |
1255 | 0 | } |