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