Rethink the EVP_PKEY cache of provider side keys
[openssl.git] / crypto / evp / m_sigver.c
1 /*
2  * Copyright 2006-2018 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 "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include <openssl/x509.h>
15 #include "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
18
19 #ifndef FIPS_MODE
20
21 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
22 {
23     EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
24     return 0;
25 }
26
27 static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
28                           const EVP_MD *type, const char *mdname,
29                           const char *props, ENGINE *e, EVP_PKEY *pkey,
30                           int ver)
31 {
32     EVP_PKEY_CTX *locpctx = NULL;
33     EVP_SIGNATURE *signature = NULL;
34     EVP_KEYMGMT *tmp_keymgmt = NULL;
35     const char *supported_sig = NULL;
36     char locmdname[80] = "";     /* 80 chars should be enough */
37     void *provkey = NULL;
38     int ret;
39
40     if (ctx->provctx != NULL) {
41         if (!ossl_assert(ctx->digest != NULL)) {
42             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
43             return 0;
44         }
45         if (ctx->digest->freectx != NULL)
46             ctx->digest->freectx(ctx->provctx);
47         ctx->provctx = NULL;
48     }
49
50     if (ctx->pctx == NULL)
51         ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
52     if (ctx->pctx == NULL)
53         return 0;
54
55     locpctx = ctx->pctx;
56     evp_pkey_ctx_free_old_ops(locpctx);
57
58     /*
59      * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
60      * calls can be removed.
61      */
62     ERR_set_mark();
63
64     if (locpctx->keytype == NULL)
65         goto legacy;
66
67     /*
68      * Ensure that the key is provided, either natively, or as a cached export.
69      *  If not, go legacy
70      */
71     tmp_keymgmt = locpctx->keymgmt;
72     provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
73                                           &tmp_keymgmt, locpctx->propquery);
74     if (provkey == NULL)
75         goto legacy;
76     if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
77         ERR_clear_last_mark();
78         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
79         goto err;
80     }
81     EVP_KEYMGMT_free(locpctx->keymgmt);
82     locpctx->keymgmt = tmp_keymgmt;
83
84     if (locpctx->keymgmt->query_operation_name != NULL)
85         supported_sig =
86             locpctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE);
87
88     /*
89      * If we didn't get a supported sig, assume there is one with the
90      * same name as the key type.
91      */
92     if (supported_sig == NULL)
93         supported_sig = locpctx->keytype;
94
95     /*
96      * Because we cleared out old ops, we shouldn't need to worry about
97      * checking if signature is already there.
98      */
99     signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
100                                     locpctx->propquery);
101
102     if (signature == NULL
103         || (EVP_KEYMGMT_provider(locpctx->keymgmt)
104             != EVP_SIGNATURE_provider(signature))) {
105         /*
106          * We don't need to free ctx->keymgmt here, as it's not necessarily
107          * tied to this operation.  It will be freed by EVP_PKEY_CTX_free().
108          */
109         EVP_SIGNATURE_free(signature);
110         goto legacy;
111     }
112
113     /*
114      * TODO remove this when legacy is gone
115      * If we don't have the full support we need with provided methods,
116      * let's go see if legacy does.
117      */
118     ERR_pop_to_mark();
119
120     /* No more legacy from here down to legacy: */
121
122     if (pctx != NULL)
123         *pctx = locpctx;
124
125     locpctx->op.sig.signature = signature;
126     locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
127                              : EVP_PKEY_OP_SIGNCTX;
128     locpctx->op.sig.sigprovctx
129         = signature->newctx(ossl_provider_ctx(signature->prov));
130     if (locpctx->op.sig.sigprovctx == NULL) {
131         ERR_raise(ERR_LIB_EVP,  EVP_R_INITIALIZATION_ERROR);
132         goto err;
133     }
134     if (type != NULL) {
135         ctx->reqdigest = type;
136         if (mdname == NULL)
137             mdname = EVP_MD_name(type);
138     } else {
139         if (mdname == NULL
140             && EVP_PKEY_get_default_digest_name(locpctx->pkey, locmdname,
141                                                 sizeof(locmdname)))
142             mdname = locmdname;
143
144         if (mdname != NULL) {
145             /*
146              * This might be requested by a later call to EVP_MD_CTX_md().
147              * In that case the "explicit fetch" rules apply for that
148              * function (as per man pages), i.e. the ref count is not updated
149              * so the EVP_MD should not be used beyound the lifetime of the
150              * EVP_MD_CTX.
151              */
152             ctx->reqdigest = ctx->fetched_digest =
153                 EVP_MD_fetch(locpctx->libctx, mdname, props);
154         }
155     }
156
157     if (ver) {
158         if (signature->digest_verify_init == NULL) {
159             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
160             goto err;
161         }
162         ret = signature->digest_verify_init(locpctx->op.sig.sigprovctx,
163                                             mdname, props, provkey);
164     } else {
165         if (signature->digest_sign_init == NULL) {
166             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
167             goto err;
168         }
169         ret = signature->digest_sign_init(locpctx->op.sig.sigprovctx,
170                                           mdname, props, provkey);
171     }
172
173     return ret ? 1 : 0;
174  err:
175     evp_pkey_ctx_free_old_ops(locpctx);
176     locpctx->operation = EVP_PKEY_OP_UNDEFINED;
177     return 0;
178
179  legacy:
180     /*
181      * TODO remove this when legacy is gone
182      * If we don't have the full support we need with provided methods,
183      * let's go see if legacy does.
184      */
185     ERR_pop_to_mark();
186
187     if (ctx->pctx->pmeth == NULL) {
188         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
189         return 0;
190     }
191
192     if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
193
194         if (type == NULL) {
195             int def_nid;
196             if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
197                 type = EVP_get_digestbynid(def_nid);
198         }
199
200         if (type == NULL) {
201             EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
202             return 0;
203         }
204     }
205
206     if (ver) {
207         if (ctx->pctx->pmeth->verifyctx_init) {
208             if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
209                 return 0;
210             ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
211         } else if (ctx->pctx->pmeth->digestverify != 0) {
212             ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
213             ctx->update = update;
214         } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
215             return 0;
216         }
217     } else {
218         if (ctx->pctx->pmeth->signctx_init) {
219             if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
220                 return 0;
221             ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
222         } else if (ctx->pctx->pmeth->digestsign != 0) {
223             ctx->pctx->operation = EVP_PKEY_OP_SIGN;
224             ctx->update = update;
225         } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
226             return 0;
227         }
228     }
229     if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
230         return 0;
231     if (pctx)
232         *pctx = ctx->pctx;
233     if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
234         return 1;
235     if (!EVP_DigestInit_ex(ctx, type, e))
236         return 0;
237     /*
238      * This indicates the current algorithm requires
239      * special treatment before hashing the tbs-message.
240      */
241     if (ctx->pctx->pmeth->digest_custom != NULL)
242         return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx);
243
244     return 1;
245 }
246
247 int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
248                           const char *mdname, const char *props, EVP_PKEY *pkey)
249 {
250     return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, 0);
251 }
252
253 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
254                        const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
255 {
256     return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, 0);
257 }
258
259 int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
260                             const char *mdname, const char *props,
261                             EVP_PKEY *pkey)
262 {
263     return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, 1);
264 }
265
266 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
267                          const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
268 {
269     return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, 1);
270 }
271 #endif /* FIPS_MDOE */
272
273 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
274 {
275     EVP_PKEY_CTX *pctx = ctx->pctx;
276
277     if (pctx == NULL
278             || pctx->operation != EVP_PKEY_OP_SIGNCTX
279             || pctx->op.sig.sigprovctx == NULL
280             || pctx->op.sig.signature == NULL)
281         goto legacy;
282
283     return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.sigprovctx,
284                                                       data, dsize);
285
286  legacy:
287     return EVP_DigestUpdate(ctx, data, dsize);
288 }
289
290 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
291 {
292     EVP_PKEY_CTX *pctx = ctx->pctx;
293
294     if (pctx == NULL
295             || pctx->operation != EVP_PKEY_OP_VERIFYCTX
296             || pctx->op.sig.sigprovctx == NULL
297             || pctx->op.sig.signature == NULL)
298         goto legacy;
299
300     return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.sigprovctx,
301                                                         data, dsize);
302
303  legacy:
304     return EVP_DigestUpdate(ctx, data, dsize);
305 }
306
307 #ifndef FIPS_MODE
308 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
309                         size_t *siglen)
310 {
311     int sctx = 0, r = 0;
312     EVP_PKEY_CTX *pctx = ctx->pctx;
313
314     if (pctx == NULL
315             || pctx->operation != EVP_PKEY_OP_SIGNCTX
316             || pctx->op.sig.sigprovctx == NULL
317             || pctx->op.sig.signature == NULL)
318         goto legacy;
319
320     return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.sigprovctx,
321                                                      sigret, siglen, SIZE_MAX);
322
323  legacy:
324     if (pctx == NULL || pctx->pmeth == NULL) {
325         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
326         return 0;
327     }
328
329     if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
330         if (sigret == NULL)
331             return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
332         if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
333             r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
334         else {
335             EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(pctx);
336
337             if (dctx == NULL)
338                 return 0;
339             r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
340             EVP_PKEY_CTX_free(dctx);
341         }
342         return r;
343     }
344     if (pctx->pmeth->signctx != NULL)
345         sctx = 1;
346     else
347         sctx = 0;
348     if (sigret != NULL) {
349         unsigned char md[EVP_MAX_MD_SIZE];
350         unsigned int mdlen = 0;
351
352         if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
353             if (sctx)
354                 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
355             else
356                 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
357         } else {
358             EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
359
360             if (tmp_ctx == NULL)
361                 return 0;
362             if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
363                 EVP_MD_CTX_free(tmp_ctx);
364                 return 0;
365             }
366             if (sctx)
367                 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
368                                                   sigret, siglen, tmp_ctx);
369             else
370                 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
371             EVP_MD_CTX_free(tmp_ctx);
372         }
373         if (sctx || !r)
374             return r;
375         if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
376             return 0;
377     } else {
378         if (sctx) {
379             if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
380                 return 0;
381         } else {
382             int s = EVP_MD_size(ctx->digest);
383
384             if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
385                 return 0;
386         }
387     }
388     return 1;
389 }
390
391 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
392                    const unsigned char *tbs, size_t tbslen)
393 {
394     if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
395         return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
396     if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
397         return 0;
398     return EVP_DigestSignFinal(ctx, sigret, siglen);
399 }
400
401 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
402                           size_t siglen)
403 {
404     unsigned char md[EVP_MAX_MD_SIZE];
405     int r = 0;
406     unsigned int mdlen = 0;
407     int vctx = 0;
408     EVP_PKEY_CTX *pctx = ctx->pctx;
409
410     if (pctx == NULL
411             || pctx->operation != EVP_PKEY_OP_VERIFYCTX
412             || pctx->op.sig.sigprovctx == NULL
413             || pctx->op.sig.signature == NULL)
414         goto legacy;
415
416     return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.sigprovctx,
417                                                        sig, siglen);
418
419  legacy:
420     if (pctx == NULL || pctx->pmeth == NULL) {
421         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
422         return 0;
423     }
424
425     if (pctx->pmeth->verifyctx != NULL)
426         vctx = 1;
427     else
428         vctx = 0;
429     if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
430         if (vctx)
431             r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
432         else
433             r = EVP_DigestFinal_ex(ctx, md, &mdlen);
434     } else {
435         EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
436         if (tmp_ctx == NULL)
437             return -1;
438         if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
439             EVP_MD_CTX_free(tmp_ctx);
440             return -1;
441         }
442         if (vctx)
443             r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
444                                                 sig, siglen, tmp_ctx);
445         else
446             r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
447         EVP_MD_CTX_free(tmp_ctx);
448     }
449     if (vctx || !r)
450         return r;
451     return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
452 }
453
454 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
455                      size_t siglen, const unsigned char *tbs, size_t tbslen)
456 {
457     if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
458         return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
459     if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
460         return -1;
461     return EVP_DigestVerifyFinal(ctx, sigret, siglen);
462 }
463 #endif /* FIPS_MODE */