]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dsa/dsa_pmeth.c
Document the change in the previous commit about loading the config file
[thirdparty/openssl.git] / crypto / dsa / dsa_pmeth.c
CommitLineData
0f113f3e 1/*
d2e9e320 2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
c927df3f 3 *
d2e9e320
RS
4 * Licensed under the OpenSSL license (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
c927df3f
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
c927df3f
DSH
12#include <openssl/asn1t.h>
13#include <openssl/x509.h>
c927df3f 14#include <openssl/evp.h>
1e26a8ba 15#include <openssl/bn.h>
27af42f9 16#include "internal/evp_int.h"
357d5de5 17#include "dsa_locl.h"
c927df3f
DSH
18
19/* DSA pkey context structure */
20
0f113f3e
MC
21typedef struct {
22 /* Parameter gen parameters */
23 int nbits; /* size of p in bits (default: 1024) */
24 int qbits; /* size of q in bits (default: 160) */
25 const EVP_MD *pmd; /* MD for parameter generation */
26 /* Keygen callback info */
27 int gentmp[2];
28 /* message digest */
29 const EVP_MD *md; /* MD for the signature */
30} DSA_PKEY_CTX;
c927df3f
DSH
31
32static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
0f113f3e 33{
e705fcf1
F
34 DSA_PKEY_CTX *dctx = OPENSSL_malloc(sizeof(*dctx));
35
90945fa3 36 if (dctx == NULL)
0f113f3e
MC
37 return 0;
38 dctx->nbits = 1024;
39 dctx->qbits = 160;
40 dctx->pmd = NULL;
41 dctx->md = NULL;
42
43 ctx->data = dctx;
44 ctx->keygen_info = dctx->gentmp;
45 ctx->keygen_info_count = 2;
46
47 return 1;
48}
c927df3f 49
8bdcef40 50static int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
0f113f3e
MC
51{
52 DSA_PKEY_CTX *dctx, *sctx;
e705fcf1 53
0f113f3e
MC
54 if (!pkey_dsa_init(dst))
55 return 0;
56 sctx = src->data;
57 dctx = dst->data;
58 dctx->nbits = sctx->nbits;
59 dctx->qbits = sctx->qbits;
60 dctx->pmd = sctx->pmd;
61 dctx->md = sctx->md;
62 return 1;
63}
8bdcef40 64
c927df3f 65static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
0f113f3e
MC
66{
67 DSA_PKEY_CTX *dctx = ctx->data;
b548a1f1 68 OPENSSL_free(dctx);
0f113f3e
MC
69}
70
71static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
72 size_t *siglen, const unsigned char *tbs,
73 size_t tbslen)
74{
a773b52a 75 int ret;
0f113f3e
MC
76 unsigned int sltmp;
77 DSA_PKEY_CTX *dctx = ctx->data;
78 DSA *dsa = ctx->pkey->pkey.dsa;
79
9d04f834
DSH
80 if (dctx->md) {
81 if (tbslen != (size_t)EVP_MD_size(dctx->md))
82 return 0;
9d04f834
DSH
83 } else {
84 if (tbslen != SHA_DIGEST_LENGTH)
85 return 0;
9d04f834 86 }
0f113f3e 87
a773b52a 88 ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
0f113f3e
MC
89
90 if (ret <= 0)
91 return ret;
92 *siglen = sltmp;
93 return 1;
94}
c927df3f
DSH
95
96static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
0f113f3e
MC
97 const unsigned char *sig, size_t siglen,
98 const unsigned char *tbs, size_t tbslen)
99{
a773b52a 100 int ret;
0f113f3e
MC
101 DSA_PKEY_CTX *dctx = ctx->data;
102 DSA *dsa = ctx->pkey->pkey.dsa;
c927df3f 103
9d04f834
DSH
104 if (dctx->md) {
105 if (tbslen != (size_t)EVP_MD_size(dctx->md))
106 return 0;
9d04f834
DSH
107 } else {
108 if (tbslen != SHA_DIGEST_LENGTH)
109 return 0;
9d04f834 110 }
c927df3f 111
a773b52a 112 ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
c927df3f 113
0f113f3e
MC
114 return ret;
115}
c927df3f
DSH
116
117static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
0f113f3e
MC
118{
119 DSA_PKEY_CTX *dctx = ctx->data;
e705fcf1 120
0f113f3e
MC
121 switch (type) {
122 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
123 if (p1 < 256)
124 return -2;
125 dctx->nbits = p1;
126 return 1;
127
128 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
129 if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
130 return -2;
131 dctx->qbits = p1;
132 return 1;
133
134 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
135 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
136 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
137 EVP_MD_type((const EVP_MD *)p2) != NID_sha256) {
138 DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
139 return 0;
140 }
8a05c4d3 141 dctx->pmd = p2;
0f113f3e
MC
142 return 1;
143
144 case EVP_PKEY_CTRL_MD:
145 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
146 EVP_MD_type((const EVP_MD *)p2) != NID_dsa &&
147 EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
148 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
149 EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
150 EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
151 EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
152 DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
153 return 0;
154 }
155 dctx->md = p2;
156 return 1;
157
158 case EVP_PKEY_CTRL_GET_MD:
159 *(const EVP_MD **)p2 = dctx->md;
160 return 1;
161
162 case EVP_PKEY_CTRL_DIGESTINIT:
163 case EVP_PKEY_CTRL_PKCS7_SIGN:
164 case EVP_PKEY_CTRL_CMS_SIGN:
165 return 1;
166
167 case EVP_PKEY_CTRL_PEER_KEY:
168 DSAerr(DSA_F_PKEY_DSA_CTRL,
169 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
170 return -2;
171 default:
172 return -2;
173
174 }
175}
176
c927df3f 177static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
0f113f3e
MC
178 const char *type, const char *value)
179{
86885c28 180 if (strcmp(type, "dsa_paramgen_bits") == 0) {
0f113f3e
MC
181 int nbits;
182 nbits = atoi(value);
183 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
184 }
86885c28 185 if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
0f113f3e
MC
186 int qbits = atoi(value);
187 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
188 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits,
189 NULL);
190 }
86885c28 191 if (strcmp(type, "dsa_paramgen_md") == 0) {
0f113f3e
MC
192 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
193 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0,
194 (void *)EVP_get_digestbyname(value));
195 }
196 return -2;
197}
c927df3f
DSH
198
199static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
0f113f3e
MC
200{
201 DSA *dsa = NULL;
202 DSA_PKEY_CTX *dctx = ctx->data;
203 BN_GENCB *pcb;
204 int ret;
e705fcf1 205
0f113f3e
MC
206 if (ctx->pkey_gencb) {
207 pcb = BN_GENCB_new();
90945fa3 208 if (pcb == NULL)
0f113f3e
MC
209 return 0;
210 evp_pkey_set_cb_translate(pcb, ctx);
211 } else
212 pcb = NULL;
213 dsa = DSA_new();
90945fa3 214 if (dsa == NULL) {
23a1d5e9 215 BN_GENCB_free(pcb);
0f113f3e
MC
216 return 0;
217 }
218 ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
219 NULL, 0, NULL, NULL, NULL, pcb);
23a1d5e9 220 BN_GENCB_free(pcb);
0f113f3e
MC
221 if (ret)
222 EVP_PKEY_assign_DSA(pkey, dsa);
223 else
224 DSA_free(dsa);
225 return ret;
226}
c927df3f 227
75ef7188 228static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
0f113f3e
MC
229{
230 DSA *dsa = NULL;
e705fcf1 231
0f113f3e
MC
232 if (ctx->pkey == NULL) {
233 DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET);
234 return 0;
235 }
236 dsa = DSA_new();
90945fa3 237 if (dsa == NULL)
0f113f3e
MC
238 return 0;
239 EVP_PKEY_assign_DSA(pkey, dsa);
240 /* Note: if error return, pkey is freed by parent routine */
241 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
242 return 0;
243 return DSA_generate_key(pkey->pkey.dsa);
244}
245
246const EVP_PKEY_METHOD dsa_pkey_meth = {
247 EVP_PKEY_DSA,
248 EVP_PKEY_FLAG_AUTOARGLEN,
249 pkey_dsa_init,
250 pkey_dsa_copy,
251 pkey_dsa_cleanup,
252
253 0,
254 pkey_dsa_paramgen,
255
256 0,
257 pkey_dsa_keygen,
258
259 0,
260 pkey_dsa_sign,
261
262 0,
263 pkey_dsa_verify,
264
265 0, 0,
266
267 0, 0, 0, 0,
268
269 0, 0,
270
271 0, 0,
272
273 0, 0,
274
275 pkey_dsa_ctrl,
276 pkey_dsa_ctrl_str
277};