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