1221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * project 2006.
3221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
4221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* ====================================================================
5221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
6221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
7221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Redistribution and use in source and binary forms, with or without
8221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * modification, are permitted provided that the following conditions
9221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * are met:
10221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
11221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 1. Redistributions of source code must retain the above copyright
12221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    notice, this list of conditions and the following disclaimer.
13221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
14221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright
15221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    notice, this list of conditions and the following disclaimer in
16221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    the documentation and/or other materials provided with the
17221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    distribution.
18221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
19221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 3. All advertising materials mentioning features or use of this
20221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    software must display the following acknowledgment:
21221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    "This product includes software developed by the OpenSSL Project
22221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
24221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    endorse or promote products derived from this software without
26221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    prior written permission. For written permission, please contact
27221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    licensing@OpenSSL.org.
28221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
29221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 5. Products derived from this software may not be called "OpenSSL"
30221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    nor may "OpenSSL" appear in their names without prior written
31221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    permission of the OpenSSL Project.
32221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
33221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 6. Redistributions of any form whatsoever must retain the following
34221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    acknowledgment:
35221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    "This product includes software developed by the OpenSSL Project
36221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
38221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * OF THE POSSIBILITY OF SUCH DAMAGE.
50221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ====================================================================
51221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
52221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * This product includes cryptographic software written by Eric Young
53221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * (eay@cryptsoft.com).  This product includes software written by Tim
54221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Hudson (tjh@cryptsoft.com).
55221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
56221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
57221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
58221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <stdio.h>
59221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "cryptlib.h"
60221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/asn1t.h>
61221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/x509.h>
62221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/evp.h>
63221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/bn.h>
64221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "evp_locl.h"
65221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "dsa_locl.h"
66221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
67221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* DSA pkey context structure */
68221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
69221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromtypedef struct
70221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
71221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Parameter gen parameters */
72221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int nbits;		/* size of p in bits (default: 1024) */
73221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int qbits;		/* size of q in bits (default: 160)  */
74221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const EVP_MD *pmd;	/* MD for parameter generation */
75221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Keygen callback info */
76221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int gentmp[2];
77221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* message digest */
78221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const EVP_MD *md;	/* MD for the signature */
79221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	} DSA_PKEY_CTX;
80221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
81221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_init(EVP_PKEY_CTX *ctx)
82221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
83221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA_PKEY_CTX *dctx;
84221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx = OPENSSL_malloc(sizeof(DSA_PKEY_CTX));
85221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!dctx)
86221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
87221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->nbits = 1024;
88221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->qbits = 160;
89221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->pmd = NULL;
90221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->md = NULL;
91221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
92221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->data = dctx;
93221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->keygen_info = dctx->gentmp;
94221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->keygen_info_count = 2;
95221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
96221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
97221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
98221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
99221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
100221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
101221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA_PKEY_CTX *dctx, *sctx;
102221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!pkey_dsa_init(dst))
103221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
104221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom       	sctx = src->data;
105221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx = dst->data;
106221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->nbits = sctx->nbits;
107221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->qbits = sctx->qbits;
108221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->pmd = sctx->pmd;
109221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dctx->md  = sctx->md;
110221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
111221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
112221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
113221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
114221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
115221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA_PKEY_CTX *dctx = ctx->data;
116221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (dctx)
117221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		OPENSSL_free(dctx);
118221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
119221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
120221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
121221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					const unsigned char *tbs, size_t tbslen)
122221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
123221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret, type;
124221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	unsigned int sltmp;
125221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA_PKEY_CTX *dctx = ctx->data;
126221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA *dsa = ctx->pkey->pkey.dsa;
127221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
128221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (dctx->md)
129221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		type = EVP_MD_type(dctx->md);
130221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
131221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		type = NID_sha1;
132221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
133221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = DSA_sign(type, tbs, tbslen, sig, &sltmp, dsa);
134221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
135221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ret <= 0)
136221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return ret;
137221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*siglen = sltmp;
138221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
139221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
140221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
141221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
142221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					const unsigned char *sig, size_t siglen,
143221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					const unsigned char *tbs, size_t tbslen)
144221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
145221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret, type;
146221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA_PKEY_CTX *dctx = ctx->data;
147221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA *dsa = ctx->pkey->pkey.dsa;
148221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
149221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (dctx->md)
150221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		type = EVP_MD_type(dctx->md);
151221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
152221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		type = NID_sha1;
153221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
154221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = DSA_verify(type, tbs, tbslen, sig, siglen, dsa);
155221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
156221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
157221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
158221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
159221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
160221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
161221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA_PKEY_CTX *dctx = ctx->data;
162221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	switch (type)
163221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
164221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
165221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (p1 < 256)
166221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return -2;
167221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		dctx->nbits = p1;
168221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
169221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
170221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
171221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
172221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return -2;
173221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		dctx->qbits = p1;
174221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
175221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
176221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
177221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1   &&
178221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		    EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
179221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		    EVP_MD_type((const EVP_MD *)p2) != NID_sha256)
180221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
181221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
182221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
183221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
184221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		dctx->md = p2;
185221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
186221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
187221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_MD:
188221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1   &&
189221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		    EVP_MD_type((const EVP_MD *)p2) != NID_dsa    &&
190221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		    EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
191221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		    EVP_MD_type((const EVP_MD *)p2) != NID_sha256)
192221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
193221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
194221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
195221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
196221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		dctx->md = p2;
197221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
198221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
199221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_DIGESTINIT:
200221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_PKCS7_SIGN:
201221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_CMS_SIGN:
202221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
203221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
204221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case EVP_PKEY_CTRL_PEER_KEY:
205221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			DSAerr(DSA_F_PKEY_DSA_CTRL,
206221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
207221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return -2;
208221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		default:
209221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -2;
210221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
211221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
212221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
213221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
214221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
215221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			const char *type, const char *value)
216221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
217221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!strcmp(type, "dsa_paramgen_bits"))
218221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
219221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		int nbits;
220221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		nbits = atoi(value);
221221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
222221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
223221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!strcmp(type, "dsa_paramgen_q_bits"))
224221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
225221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		int qbits = atoi(value);
226221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
227221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		                         EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits, NULL);
228221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
229221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!strcmp(type, "dsa_paramgen_md"))
230221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
231221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
232221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		                         EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0,
233221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		                         (void *)EVP_get_digestbyname(value));
234221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
235221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return -2;
236221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
237221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
238221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
239221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
240221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA *dsa = NULL;
241221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA_PKEY_CTX *dctx = ctx->data;
242221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BN_GENCB *pcb, cb;
243221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret;
244221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ctx->pkey_gencb)
245221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
246221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		pcb = &cb;
247221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		evp_pkey_set_cb_translate(pcb, ctx);
248221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
249221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
250221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		pcb = NULL;
251221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dsa = DSA_new();
252221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!dsa)
253221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
254221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
255221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	                           NULL, 0, NULL, NULL, pcb);
256221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ret)
257221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVP_PKEY_assign_DSA(pkey, dsa);
258221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
259221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		DSA_free(dsa);
260221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
261221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
262221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
263221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
264221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
265221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	DSA *dsa = NULL;
266221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ctx->pkey == NULL)
267221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
268221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET);
269221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
270221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
271221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	dsa = DSA_new();
272221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!dsa)
273221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
274221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	EVP_PKEY_assign_DSA(pkey, dsa);
275221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Note: if error return, pkey is freed by parent routine */
276221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
277221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
278221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return DSA_generate_key(pkey->pkey.dsa);
279221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
280221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
281221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromconst EVP_PKEY_METHOD dsa_pkey_meth =
282221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
283221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	EVP_PKEY_DSA,
284221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	EVP_PKEY_FLAG_AUTOARGLEN,
285221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_init,
286221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_copy,
287221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_cleanup,
288221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
289221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,
290221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_paramgen,
291221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
292221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,
293221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_keygen,
294221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
295221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,
296221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_sign,
297221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
298221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,
299221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_verify,
300221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
301221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,0,
302221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
303221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,0,0,0,
304221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
305221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,0,
306221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
307221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,0,
308221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
309221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,0,
310221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
311221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_ctrl,
312221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pkey_dsa_ctrl_str
313221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
314221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
315221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	};
316