]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
28428130 | 2 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
c927df3f | 3 | * |
3cdbea65 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
d2e9e320 RS |
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 |
21 | typedef 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 | |
32 | static 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 | 50 | static 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 | 65 | static 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 | ||
71 | static 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 | ||
665d9d1c BD |
80 | if (dctx->md != NULL && tbslen != (size_t)EVP_MD_size(dctx->md)) |
81 | return 0; | |
0f113f3e | 82 | |
a773b52a | 83 | ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa); |
0f113f3e MC |
84 | |
85 | if (ret <= 0) | |
86 | return ret; | |
87 | *siglen = sltmp; | |
88 | return 1; | |
89 | } | |
c927df3f DSH |
90 | |
91 | static int pkey_dsa_verify(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
92 | const unsigned char *sig, size_t siglen, |
93 | const unsigned char *tbs, size_t tbslen) | |
94 | { | |
a773b52a | 95 | int ret; |
0f113f3e MC |
96 | DSA_PKEY_CTX *dctx = ctx->data; |
97 | DSA *dsa = ctx->pkey->pkey.dsa; | |
c927df3f | 98 | |
665d9d1c BD |
99 | if (dctx->md != NULL && tbslen != (size_t)EVP_MD_size(dctx->md)) |
100 | return 0; | |
c927df3f | 101 | |
a773b52a | 102 | ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa); |
c927df3f | 103 | |
0f113f3e MC |
104 | return ret; |
105 | } | |
c927df3f DSH |
106 | |
107 | static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | |
0f113f3e MC |
108 | { |
109 | DSA_PKEY_CTX *dctx = ctx->data; | |
e705fcf1 | 110 | |
0f113f3e MC |
111 | switch (type) { |
112 | case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS: | |
113 | if (p1 < 256) | |
114 | return -2; | |
115 | dctx->nbits = p1; | |
116 | return 1; | |
117 | ||
118 | case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS: | |
119 | if (p1 != 160 && p1 != 224 && p1 && p1 != 256) | |
120 | return -2; | |
121 | dctx->qbits = p1; | |
122 | return 1; | |
123 | ||
124 | case EVP_PKEY_CTRL_DSA_PARAMGEN_MD: | |
125 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && | |
126 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && | |
127 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256) { | |
128 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); | |
129 | return 0; | |
130 | } | |
8a05c4d3 | 131 | dctx->pmd = p2; |
0f113f3e MC |
132 | return 1; |
133 | ||
134 | case EVP_PKEY_CTRL_MD: | |
135 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && | |
136 | EVP_MD_type((const EVP_MD *)p2) != NID_dsa && | |
137 | EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA && | |
138 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && | |
139 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256 && | |
140 | EVP_MD_type((const EVP_MD *)p2) != NID_sha384 && | |
141 | EVP_MD_type((const EVP_MD *)p2) != NID_sha512) { | |
142 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); | |
143 | return 0; | |
144 | } | |
145 | dctx->md = p2; | |
146 | return 1; | |
147 | ||
148 | case EVP_PKEY_CTRL_GET_MD: | |
149 | *(const EVP_MD **)p2 = dctx->md; | |
150 | return 1; | |
151 | ||
152 | case EVP_PKEY_CTRL_DIGESTINIT: | |
153 | case EVP_PKEY_CTRL_PKCS7_SIGN: | |
154 | case EVP_PKEY_CTRL_CMS_SIGN: | |
155 | return 1; | |
156 | ||
157 | case EVP_PKEY_CTRL_PEER_KEY: | |
158 | DSAerr(DSA_F_PKEY_DSA_CTRL, | |
159 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
160 | return -2; | |
161 | default: | |
162 | return -2; | |
163 | ||
164 | } | |
165 | } | |
166 | ||
c927df3f | 167 | static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
168 | const char *type, const char *value) |
169 | { | |
86885c28 | 170 | if (strcmp(type, "dsa_paramgen_bits") == 0) { |
0f113f3e MC |
171 | int nbits; |
172 | nbits = atoi(value); | |
173 | return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); | |
174 | } | |
86885c28 | 175 | if (strcmp(type, "dsa_paramgen_q_bits") == 0) { |
0f113f3e | 176 | int qbits = atoi(value); |
a97faad7 | 177 | return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, qbits); |
0f113f3e | 178 | } |
86885c28 | 179 | if (strcmp(type, "dsa_paramgen_md") == 0) { |
06d3b485 MC |
180 | const EVP_MD *md = EVP_get_digestbyname(value); |
181 | ||
182 | if (md == NULL) { | |
183 | DSAerr(DSA_F_PKEY_DSA_CTRL_STR, DSA_R_INVALID_DIGEST_TYPE); | |
184 | return 0; | |
185 | } | |
a97faad7 | 186 | return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, md); |
0f113f3e MC |
187 | } |
188 | return -2; | |
189 | } | |
c927df3f DSH |
190 | |
191 | static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | |
0f113f3e MC |
192 | { |
193 | DSA *dsa = NULL; | |
194 | DSA_PKEY_CTX *dctx = ctx->data; | |
195 | BN_GENCB *pcb; | |
196 | int ret; | |
e705fcf1 | 197 | |
0f113f3e MC |
198 | if (ctx->pkey_gencb) { |
199 | pcb = BN_GENCB_new(); | |
90945fa3 | 200 | if (pcb == NULL) |
0f113f3e MC |
201 | return 0; |
202 | evp_pkey_set_cb_translate(pcb, ctx); | |
203 | } else | |
204 | pcb = NULL; | |
205 | dsa = DSA_new(); | |
90945fa3 | 206 | if (dsa == NULL) { |
23a1d5e9 | 207 | BN_GENCB_free(pcb); |
0f113f3e MC |
208 | return 0; |
209 | } | |
210 | ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd, | |
211 | NULL, 0, NULL, NULL, NULL, pcb); | |
23a1d5e9 | 212 | BN_GENCB_free(pcb); |
0f113f3e MC |
213 | if (ret) |
214 | EVP_PKEY_assign_DSA(pkey, dsa); | |
215 | else | |
216 | DSA_free(dsa); | |
217 | return ret; | |
218 | } | |
c927df3f | 219 | |
75ef7188 | 220 | static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
0f113f3e MC |
221 | { |
222 | DSA *dsa = NULL; | |
e705fcf1 | 223 | |
0f113f3e MC |
224 | if (ctx->pkey == NULL) { |
225 | DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET); | |
226 | return 0; | |
227 | } | |
228 | dsa = DSA_new(); | |
90945fa3 | 229 | if (dsa == NULL) |
0f113f3e MC |
230 | return 0; |
231 | EVP_PKEY_assign_DSA(pkey, dsa); | |
232 | /* Note: if error return, pkey is freed by parent routine */ | |
233 | if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) | |
234 | return 0; | |
235 | return DSA_generate_key(pkey->pkey.dsa); | |
236 | } | |
237 | ||
238 | const EVP_PKEY_METHOD dsa_pkey_meth = { | |
239 | EVP_PKEY_DSA, | |
240 | EVP_PKEY_FLAG_AUTOARGLEN, | |
241 | pkey_dsa_init, | |
242 | pkey_dsa_copy, | |
243 | pkey_dsa_cleanup, | |
244 | ||
245 | 0, | |
246 | pkey_dsa_paramgen, | |
247 | ||
248 | 0, | |
249 | pkey_dsa_keygen, | |
250 | ||
251 | 0, | |
252 | pkey_dsa_sign, | |
253 | ||
254 | 0, | |
255 | pkey_dsa_verify, | |
256 | ||
257 | 0, 0, | |
258 | ||
259 | 0, 0, 0, 0, | |
260 | ||
261 | 0, 0, | |
262 | ||
263 | 0, 0, | |
264 | ||
265 | 0, 0, | |
266 | ||
267 | pkey_dsa_ctrl, | |
268 | pkey_dsa_ctrl_str | |
269 | }; |