Don't call EVP_MD_CTX_reset during EVP_DigestFinal
[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 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
20 {
21     EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
22     return 0;
23 }
24
25 static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
26                           const EVP_MD *type, const char *mdname,
27                           const char *props, ENGINE *e, EVP_PKEY *pkey,
28                           EVP_SIGNATURE *signature, int ver)
29 {
30     EVP_PKEY_CTX *locpctx = NULL;
31     void *provkey = NULL;
32     int ret;
33
34     if (ctx->provctx != NULL) {
35         if (!ossl_assert(ctx->digest != NULL)) {
36             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
37             return 0;
38         }
39         if (ctx->digest->freectx != NULL)
40             ctx->digest->freectx(ctx->provctx);
41         ctx->provctx = NULL;
42     }
43
44     if (ctx->pctx == NULL) {
45         ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
46         if (ctx->pctx == NULL)
47             return 0;
48     } else if (pkey != NULL) {
49         if (!EVP_PKEY_up_ref(pkey))
50             return 0;
51         EVP_PKEY_free(ctx->pctx->pkey);
52         ctx->pctx->pkey = pkey;
53     }
54     locpctx = ctx->pctx;
55     evp_pkey_ctx_free_old_ops(locpctx);
56     if (locpctx->pkey == NULL)
57         goto legacy;
58
59     if (e != NULL || locpctx->engine != NULL)
60         goto legacy;
61
62     if (signature != NULL) {
63         if (!EVP_SIGNATURE_up_ref(signature))
64             goto err;
65     } else {
66         /*
67          * TODO(3.0): Check for legacy handling. Remove this once all all
68          * algorithms are moved to providers.
69          */
70         switch (locpctx->pkey->type) {
71         case NID_dsa:
72             break;
73         default:
74             goto legacy;
75         }
76         signature
77             = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(locpctx->pkey->type), NULL);
78
79         if (signature == NULL) {
80             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
81             goto err;
82         }
83     }
84     locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
85                              : EVP_PKEY_OP_SIGNCTX;
86
87     locpctx->op.sig.signature = signature;
88
89     locpctx->op.sig.sigprovctx
90         = signature->newctx(ossl_provider_ctx(signature->prov));
91     if (locpctx->op.sig.sigprovctx == NULL) {
92         ERR_raise(ERR_LIB_EVP,  EVP_R_INITIALIZATION_ERROR);
93         goto err;
94     }
95     provkey = evp_keymgmt_export_to_provider(locpctx->pkey, signature->keymgmt);
96     if (provkey == NULL) {
97         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
98         goto err;
99     }
100
101     if (mdname == NULL) {
102         mdname = EVP_MD_name(type);
103         ctx->reqdigest = type;
104     } else {
105         /*
106          * This might be requested by a later call to EVP_MD_CTX_md(). In that
107          * case the "explicit fetch" rules apply for that function (as per
108          * man pages), i.e. the ref count is not updated so the EVP_MD should
109          * not be used beyound the lifetime of the EVP_MD_CTX.
110          */
111         ctx->reqdigest
112             = ctx->fetched_digest
113             = EVP_MD_fetch(
114                 ossl_provider_library_context(EVP_SIGNATURE_provider(signature)),
115                 mdname, props);
116     }
117
118     if (ver) {
119         if (signature->digest_verify_init == NULL) {
120             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
121             goto err;
122         }
123         ret = signature->digest_verify_init(locpctx->op.sig.sigprovctx, mdname,
124                                             props, provkey);
125     } else {
126         if (signature->digest_sign_init == NULL) {
127             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
128             goto err;
129         }
130         ret = signature->digest_sign_init(locpctx->op.sig.sigprovctx, mdname,
131                                           props, provkey);
132     }
133
134     return ret ? 1 : 0;
135  err:
136     evp_pkey_ctx_free_old_ops(locpctx);
137     locpctx->operation = EVP_PKEY_OP_UNDEFINED;
138     return 0;
139
140  legacy:
141     if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
142
143         if (type == NULL) {
144             int def_nid;
145             if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
146                 type = EVP_get_digestbynid(def_nid);
147         }
148
149         if (type == NULL) {
150             EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
151             return 0;
152         }
153     }
154
155     if (ver) {
156         if (ctx->pctx->pmeth->verifyctx_init) {
157             if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
158                 return 0;
159             ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
160         } else if (ctx->pctx->pmeth->digestverify != 0) {
161             ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
162             ctx->update = update;
163         } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
164             return 0;
165         }
166     } else {
167         if (ctx->pctx->pmeth->signctx_init) {
168             if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
169                 return 0;
170             ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
171         } else if (ctx->pctx->pmeth->digestsign != 0) {
172             ctx->pctx->operation = EVP_PKEY_OP_SIGN;
173             ctx->update = update;
174         } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
175             return 0;
176         }
177     }
178     if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
179         return 0;
180     if (pctx)
181         *pctx = ctx->pctx;
182     if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
183         return 1;
184     if (!EVP_DigestInit_ex(ctx, type, e))
185         return 0;
186     /*
187      * This indicates the current algorithm requires
188      * special treatment before hashing the tbs-message.
189      */
190     if (ctx->pctx->pmeth->digest_custom != NULL)
191         return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx);
192
193     return 1;
194 }
195
196 int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
197                           const char *mdname, const char *props, EVP_PKEY *pkey,
198                           EVP_SIGNATURE *signature)
199 {
200     return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, signature,
201                           0);
202 }
203
204 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
205                        const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
206 {
207     return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 0);
208 }
209
210 int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
211                             const char *mdname, const char *props,
212                             EVP_PKEY *pkey, EVP_SIGNATURE *signature)
213 {
214     return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, signature,
215                           1);
216 }
217
218 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
219                          const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
220 {
221     return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 1);
222 }
223
224 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
225 {
226     EVP_PKEY_CTX *pctx = ctx->pctx;
227
228     if (pctx == NULL
229             || pctx->operation != EVP_PKEY_OP_SIGNCTX
230             || pctx->op.sig.sigprovctx == NULL
231             || pctx->op.sig.signature == NULL)
232         goto legacy;
233
234     return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.sigprovctx,
235                                                       data, dsize);
236
237  legacy:
238     return EVP_DigestUpdate(ctx, data, dsize);
239 }
240
241 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
242 {
243     EVP_PKEY_CTX *pctx = ctx->pctx;
244
245     if (pctx == NULL
246             || pctx->operation != EVP_PKEY_OP_VERIFYCTX
247             || pctx->op.sig.sigprovctx == NULL
248             || pctx->op.sig.signature == NULL)
249         goto legacy;
250
251     return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.sigprovctx,
252                                                         data, dsize);
253
254  legacy:
255     return EVP_DigestUpdate(ctx, data, dsize);
256 }
257
258
259 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
260                         size_t *siglen)
261 {
262     int sctx = 0, r = 0;
263     EVP_PKEY_CTX *pctx = ctx->pctx;
264
265     if (pctx == NULL
266             || pctx->operation != EVP_PKEY_OP_SIGNCTX
267             || pctx->op.sig.sigprovctx == NULL
268             || pctx->op.sig.signature == NULL)
269         goto legacy;
270
271     return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.sigprovctx,
272                                                      sigret, siglen, SIZE_MAX);
273
274  legacy:
275     if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
276         if (!sigret)
277             return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
278         if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
279             r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
280         else {
281             EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);
282             if (!dctx)
283                 return 0;
284             r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
285             EVP_PKEY_CTX_free(dctx);
286         }
287         return r;
288     }
289     if (pctx->pmeth->signctx)
290         sctx = 1;
291     else
292         sctx = 0;
293     if (sigret) {
294         unsigned char md[EVP_MAX_MD_SIZE];
295         unsigned int mdlen = 0;
296         if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
297             if (sctx)
298                 r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);
299             else
300                 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
301         } else {
302             EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
303             if (tmp_ctx == NULL)
304                 return 0;
305             if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
306                 EVP_MD_CTX_free(tmp_ctx);
307                 return 0;
308             }
309             if (sctx)
310                 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
311                                                   sigret, siglen, tmp_ctx);
312             else
313                 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
314             EVP_MD_CTX_free(tmp_ctx);
315         }
316         if (sctx || !r)
317             return r;
318         if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
319             return 0;
320     } else {
321         if (sctx) {
322             if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
323                 return 0;
324         } else {
325             int s = EVP_MD_size(ctx->digest);
326             if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
327                 return 0;
328         }
329     }
330     return 1;
331 }
332
333 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
334                    const unsigned char *tbs, size_t tbslen)
335 {
336     if (ctx->pctx->pmeth->digestsign != NULL)
337         return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
338     if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
339         return 0;
340     return EVP_DigestSignFinal(ctx, sigret, siglen);
341 }
342
343 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
344                           size_t siglen)
345 {
346     unsigned char md[EVP_MAX_MD_SIZE];
347     int r = 0;
348     unsigned int mdlen = 0;
349     int vctx = 0;
350     EVP_PKEY_CTX *pctx = ctx->pctx;
351
352     if (pctx == NULL
353             || pctx->operation != EVP_PKEY_OP_VERIFYCTX
354             || pctx->op.sig.sigprovctx == NULL
355             || pctx->op.sig.signature == NULL)
356         goto legacy;
357
358     return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.sigprovctx,
359                                                        sig, siglen);
360
361  legacy:
362     if (ctx->pctx->pmeth->verifyctx)
363         vctx = 1;
364     else
365         vctx = 0;
366     if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
367         if (vctx)
368             r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);
369         else
370             r = EVP_DigestFinal_ex(ctx, md, &mdlen);
371     } else {
372         EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
373         if (tmp_ctx == NULL)
374             return -1;
375         if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
376             EVP_MD_CTX_free(tmp_ctx);
377             return -1;
378         }
379         if (vctx)
380             r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
381                                                 sig, siglen, tmp_ctx);
382         else
383             r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
384         EVP_MD_CTX_free(tmp_ctx);
385     }
386     if (vctx || !r)
387         return r;
388     return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
389 }
390
391 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
392                      size_t siglen, const unsigned char *tbs, size_t tbslen)
393 {
394     if (ctx->pctx->pmeth->digestverify != NULL)
395         return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
396     if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
397         return -1;
398     return EVP_DigestVerifyFinal(ctx, sigret, siglen);
399 }