xref: /openbsd-src/lib/libcrypto/dsa/dsa_pmeth.c (revision a5aac4c5c2beb790313ed29c491b886895baaece)
1 /* $OpenBSD: dsa_pmeth.c,v 1.21 2024/10/19 14:39:44 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 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 <limits.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 
64 #include <openssl/asn1t.h>
65 #include <openssl/bn.h>
66 #include <openssl/err.h>
67 #include <openssl/evp.h>
68 #include <openssl/x509.h>
69 
70 #include "bn_local.h"
71 #include "dsa_local.h"
72 #include "evp_local.h"
73 
74 /* DSA pkey context structure */
75 
76 typedef struct {
77 	/* Parameter gen parameters */
78 	int nbits;		/* size of p in bits (default: 1024) */
79 	int qbits;		/* size of q in bits (default: 160)  */
80 	const EVP_MD *pmd;	/* MD for parameter generation */
81 	/* Keygen callback info */
82 	int gentmp[2];
83 	/* message digest */
84 	const EVP_MD *md;	/* MD for the signature */
85 } DSA_PKEY_CTX;
86 
87 static int
88 pkey_dsa_init(EVP_PKEY_CTX *ctx)
89 {
90 	DSA_PKEY_CTX *dctx;
91 
92 	dctx = malloc(sizeof(DSA_PKEY_CTX));
93 	if (!dctx)
94 		return 0;
95 	dctx->nbits = 1024;
96 	dctx->qbits = 160;
97 	dctx->pmd = NULL;
98 	dctx->md = NULL;
99 
100 	ctx->data = dctx;
101 	ctx->keygen_info = dctx->gentmp;
102 	ctx->keygen_info_count = 2;
103 
104 	return 1;
105 }
106 
107 static int
108 pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
109 {
110 	DSA_PKEY_CTX *dctx, *sctx;
111 
112 	if (!pkey_dsa_init(dst))
113 		return 0;
114 	sctx = src->data;
115 	dctx = dst->data;
116 	dctx->nbits = sctx->nbits;
117 	dctx->qbits = sctx->qbits;
118 	dctx->pmd = sctx->pmd;
119 	dctx->md  = sctx->md;
120 	return 1;
121 }
122 
123 static void
124 pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
125 {
126 	DSA_PKEY_CTX *dctx = ctx->data;
127 
128 	free(dctx);
129 }
130 
131 static int
132 pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *out_siglen,
133     const unsigned char *tbs, size_t tbslen)
134 {
135 	DSA *dsa = ctx->pkey->pkey.dsa;
136 	DSA_PKEY_CTX *dctx = ctx->data;
137 	unsigned int siglen;
138 
139 	*out_siglen = 0;
140 
141 	if (tbslen > INT_MAX)
142 		 return 0;
143 
144 	if (dctx->md != NULL) {
145 		if (tbslen != EVP_MD_size(dctx->md))
146 			return 0;
147 	}
148 
149 	if (!DSA_sign(0, tbs, tbslen, sig, &siglen, dsa))
150 		return 0;
151 
152 	*out_siglen = siglen;
153 
154 	return 1;
155 }
156 
157 static int
158 pkey_dsa_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
159     const unsigned char *tbs, size_t tbslen)
160 {
161 	DSA *dsa = ctx->pkey->pkey.dsa;
162 	DSA_PKEY_CTX *dctx = ctx->data;
163 
164 	if (tbslen > INT_MAX || siglen > INT_MAX)
165 		 return 0;
166 
167 	if (dctx->md != NULL) {
168 		if (tbslen != EVP_MD_size(dctx->md))
169 			return 0;
170 	}
171 
172 	return DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
173 }
174 
175 static int
176 pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
177 {
178 	DSA_PKEY_CTX *dctx = ctx->data;
179 
180 	switch (type) {
181 	case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
182 		if (p1 < 256)
183 			return -2;
184 		dctx->nbits = p1;
185 		return 1;
186 
187 	case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
188 		if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
189 			return -2;
190 		dctx->qbits = p1;
191 		return 1;
192 
193 	case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
194 		switch (EVP_MD_type((const EVP_MD *)p2)) {
195 		case NID_sha1:
196 		case NID_sha224:
197 		case NID_sha256:
198 			break;
199 		default:
200 			DSAerror(DSA_R_INVALID_DIGEST_TYPE);
201 			return 0;
202 		}
203 		dctx->md = p2;
204 		return 1;
205 
206 	case EVP_PKEY_CTRL_MD:
207 		/* ANSI X9.57 and NIST CSOR. */
208 		switch (EVP_MD_type(p2)) {
209 		case NID_sha1:
210 		case NID_dsa:
211 		case NID_dsaWithSHA:
212 		case NID_sha224:
213 		case NID_sha256:
214 		case NID_sha384:
215 		case NID_sha512:
216 		case NID_sha3_224:
217 		case NID_sha3_256:
218 		case NID_sha3_384:
219 		case NID_sha3_512:
220 			break;
221 		default:
222 			DSAerror(DSA_R_INVALID_DIGEST_TYPE);
223 			return 0;
224 		}
225 		dctx->md = p2;
226 		return 1;
227 
228 	case EVP_PKEY_CTRL_GET_MD:
229 		*(const EVP_MD **)p2 = dctx->md;
230 		return 1;
231 
232 	case EVP_PKEY_CTRL_DIGESTINIT:
233 	case EVP_PKEY_CTRL_PKCS7_SIGN:
234 	case EVP_PKEY_CTRL_CMS_SIGN:
235 		return 1;
236 
237 	case EVP_PKEY_CTRL_PEER_KEY:
238 		DSAerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
239 		return -2;
240 	default:
241 		return -2;
242 	}
243 }
244 
245 static int
246 pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
247 {
248 	const char *errstr;
249 
250 	if (!strcmp(type, "dsa_paramgen_bits")) {
251 		int nbits;
252 
253 		nbits = strtonum(value, INT_MIN, INT_MAX, &errstr);
254 		if (errstr != NULL)
255 			return -2;
256 		return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
257 	} else if (!strcmp(type, "dsa_paramgen_q_bits")) {
258 		int qbits;
259 
260 		qbits = strtonum(value, INT_MIN, INT_MAX, &errstr);
261 		if (errstr != NULL)
262 			return -2;
263 		return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA,
264 		    EVP_PKEY_OP_PARAMGEN, EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS,
265 		    qbits, NULL);
266 	} else if (!strcmp(type, "dsa_paramgen_md")) {
267 		return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA,
268 		    EVP_PKEY_OP_PARAMGEN, EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0,
269 		    (void *)EVP_get_digestbyname(value));
270 	}
271 
272 	return -2;
273 }
274 
275 static int
276 pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
277 {
278 	DSA *dsa;
279 	DSA_PKEY_CTX *dctx = ctx->data;
280 	BN_GENCB *pcb = NULL;
281 	BN_GENCB cb = {0};
282 	int ret = 0;
283 
284 	if ((dsa = DSA_new()) == NULL)
285 		goto err;
286 	if (ctx->pkey_gencb != NULL) {
287 		pcb = &cb;
288 		evp_pkey_set_cb_translate(pcb, ctx);
289 	}
290 	if (!dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
291 	    NULL, 0, NULL, NULL, NULL, pcb))
292 		goto err;
293 	if (!EVP_PKEY_assign_DSA(pkey, dsa))
294 		goto err;
295 	dsa = NULL;
296 
297 	ret = 1;
298 
299  err:
300 	DSA_free(dsa);
301 
302 	return ret;
303 }
304 
305 static int
306 pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
307 {
308 	DSA *dsa = NULL;
309 	int ret = 0;
310 
311 	if (ctx->pkey == NULL) {
312 		DSAerror(DSA_R_NO_PARAMETERS_SET);
313 		goto err;
314 	}
315 	if ((dsa = DSA_new()) == NULL)
316 		goto err;
317 	if (!EVP_PKEY_set1_DSA(pkey, dsa))
318 		goto err;
319 
320 	if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
321 		goto err;
322 	if (!DSA_generate_key(dsa))
323 		goto err;
324 
325 	ret = 1;
326 
327  err:
328 	DSA_free(dsa);
329 
330 	return ret;
331 }
332 
333 const EVP_PKEY_METHOD dsa_pkey_meth = {
334 	.pkey_id = EVP_PKEY_DSA,
335 	.flags = EVP_PKEY_FLAG_AUTOARGLEN,
336 
337 	.init = pkey_dsa_init,
338 	.copy = pkey_dsa_copy,
339 	.cleanup = pkey_dsa_cleanup,
340 
341 	.paramgen = pkey_dsa_paramgen,
342 
343 	.keygen = pkey_dsa_keygen,
344 
345 	.sign = pkey_dsa_sign,
346 
347 	.verify = pkey_dsa_verify,
348 
349 	.ctrl = pkey_dsa_ctrl,
350 	.ctrl_str = pkey_dsa_ctrl_str
351 };
352