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