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