xref: /openbsd-src/lib/libcrypto/evp/m_sigver.c (revision e89c16ec68b9f3d05a0965f4c9c4ee76fae4a65a)
1 /* $OpenBSD: m_sigver.c,v 1.21 2024/03/27 01:55:40 joshua Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006,2007 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/err.h>
62 #include <openssl/evp.h>
63 #include <openssl/objects.h>
64 #include <openssl/x509.h>
65 
66 #include "evp_local.h"
67 
68 static int
69 update_oneshot_only(EVP_MD_CTX *ctx, const void *data, size_t datalen)
70 {
71 	EVPerror(EVP_R_ONLY_ONESHOT_SUPPORTED);
72 	return 0;
73 }
74 
75 static int
76 do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type,
77     EVP_PKEY *pkey, int ver)
78 {
79 	if (ctx->pctx == NULL)
80 		ctx->pctx = EVP_PKEY_CTX_new(pkey, NULL);
81 	if (ctx->pctx == NULL)
82 		return 0;
83 
84 	if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
85 		if (type == NULL) {
86 			int def_nid;
87 			if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
88 				type = EVP_get_digestbynid(def_nid);
89 		}
90 
91 		if (type == NULL) {
92 			EVPerror(EVP_R_NO_DEFAULT_DIGEST);
93 			return 0;
94 		}
95 	}
96 
97 	if (ver) {
98 		if (ctx->pctx->pmeth->digestverify != NULL) {
99 			ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
100 			ctx->update = update_oneshot_only;
101 		} else if (EVP_PKEY_verify_init(ctx->pctx) <= 0)
102 			return 0;
103 	} else {
104 		if (ctx->pctx->pmeth->signctx_init) {
105 			if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
106 				return 0;
107 			ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
108 		} else if (ctx->pctx->pmeth->digestsign != NULL) {
109 			ctx->pctx->operation = EVP_PKEY_OP_SIGN;
110 			ctx->update = update_oneshot_only;
111 		} else if (EVP_PKEY_sign_init(ctx->pctx) <= 0)
112 			return 0;
113 	}
114 	if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
115 		return 0;
116 	if (pctx)
117 		*pctx = ctx->pctx;
118 	if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
119 		return 1;
120 	if (!EVP_DigestInit_ex(ctx, type, NULL))
121 		return 0;
122 	return 1;
123 }
124 
125 int
126 EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type,
127     ENGINE *e, EVP_PKEY *pkey)
128 {
129 	return do_sigver_init(ctx, pctx, type, pkey, 0);
130 }
131 
132 int
133 EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type,
134     ENGINE *e, EVP_PKEY *pkey)
135 {
136 	return do_sigver_init(ctx, pctx, type, pkey, 1);
137 }
138 
139 static int
140 evp_digestsignfinal_sigctx_custom(EVP_MD_CTX *ctx, unsigned char *sigret,
141     size_t *siglen)
142 {
143 	EVP_PKEY_CTX *pctx = ctx->pctx;
144 	EVP_PKEY_CTX *dctx = NULL;
145 	int ret = 0;
146 
147 	if (sigret == NULL)
148 		return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
149 
150 	/* XXX - support EVP_MD_CTX_FLAG_FINALISE? */
151 	if ((dctx = EVP_PKEY_CTX_dup(pctx)) == NULL)
152 		goto err;
153 
154 	if (!dctx->pmeth->signctx(dctx, sigret, siglen, ctx))
155 		goto err;
156 
157 	ret = 1;
158 
159  err:
160 	EVP_PKEY_CTX_free(dctx);
161 
162 	return ret;
163 }
164 
165 int
166 EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen)
167 {
168 	EVP_PKEY_CTX *pctx = ctx->pctx;
169 	EVP_MD_CTX *md_ctx = NULL;
170 	unsigned char md[EVP_MAX_MD_SIZE];
171 	unsigned int mdlen = 0;
172 	int s;
173 	int ret = 0;
174 
175 	if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
176 		return evp_digestsignfinal_sigctx_custom(ctx, sigret, siglen);
177 
178 	if (sigret == NULL) {
179 		if (ctx->pctx->pmeth->signctx != NULL) {
180 			if (ctx->pctx->pmeth->signctx(ctx->pctx, NULL,
181 			    siglen, ctx) <= 0)
182 				return 0;
183 			return 1;
184 		}
185 
186 		if ((s = EVP_MD_size(ctx->digest)) < 0)
187 			return 0;
188 		if (EVP_PKEY_sign(ctx->pctx, NULL, siglen, NULL, s) <= 0)
189 		       	return 0;
190 
191 		return 1;
192 	}
193 
194 
195 	if ((md_ctx = EVP_MD_CTX_new()) == NULL)
196 		goto err;
197 	if (!EVP_MD_CTX_copy_ex(md_ctx, ctx))
198 		goto err;
199 	if (md_ctx->pctx->pmeth->signctx != NULL) {
200 		if(md_ctx->pctx->pmeth->signctx(md_ctx->pctx,
201 		    sigret, siglen, md_ctx) <= 0)
202 			goto err;
203 	}
204 	if (!EVP_DigestFinal_ex(md_ctx, md, &mdlen))
205 		goto err;
206 	if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
207 		goto err;
208 
209 	ret = 1;
210 
211  err:
212 	EVP_MD_CTX_free(md_ctx);
213 
214 	return ret;
215 }
216 
217 int
218 EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
219     const unsigned char *tbs, size_t tbslen)
220 {
221 	if (ctx->pctx->pmeth->digestsign != NULL)
222 		return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen,
223 		    tbs, tbslen);
224 
225 	if (sigret != NULL) {
226 		if (EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
227 			return 0;
228 	}
229 
230 	return EVP_DigestSignFinal(ctx, sigret, siglen);
231 }
232 
233 int
234 EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen)
235 {
236 	EVP_MD_CTX tmp_ctx;
237 	unsigned char md[EVP_MAX_MD_SIZE];
238 	int r;
239 	unsigned int mdlen = 0;
240 
241 	EVP_MD_CTX_legacy_clear(&tmp_ctx);
242 	if (!EVP_MD_CTX_copy_ex(&tmp_ctx, ctx))
243 		return -1;
244 	r = EVP_DigestFinal_ex(&tmp_ctx, md, &mdlen);
245 	EVP_MD_CTX_cleanup(&tmp_ctx);
246 	if (!r)
247 		return r;
248 	return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
249 }
250 
251 int
252 EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, size_t siglen,
253     const unsigned char *tbs, size_t tbslen)
254 {
255 	if (ctx->pctx->pmeth->digestverify != NULL)
256 		return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen,
257 		    tbs, tbslen);
258 
259 	if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
260 		return -1;
261 
262 	return EVP_DigestVerifyFinal(ctx, sigret, siglen);
263 }
264