diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_pmeth.c | 162 |
1 files changed, 84 insertions, 78 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_pmeth.c b/src/lib/libcrypto/dsa/dsa_pmeth.c index e75f0153de..f013a3f6e7 100644 --- a/src/lib/libcrypto/dsa/dsa_pmeth.c +++ b/src/lib/libcrypto/dsa/dsa_pmeth.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_pmeth.c,v 1.6 2014/06/12 20:40:57 deraadt Exp $ */ | 1 | /* $OpenBSD: dsa_pmeth.c,v 1.7 2014/07/09 10:16:24 miod Exp $ */ |
2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | * project 2006. | 3 | * project 2006. |
4 | */ | 4 | */ |
@@ -68,8 +68,7 @@ | |||
68 | 68 | ||
69 | /* DSA pkey context structure */ | 69 | /* DSA pkey context structure */ |
70 | 70 | ||
71 | typedef struct | 71 | typedef struct { |
72 | { | ||
73 | /* Parameter gen parameters */ | 72 | /* Parameter gen parameters */ |
74 | int nbits; /* size of p in bits (default: 1024) */ | 73 | int nbits; /* size of p in bits (default: 1024) */ |
75 | int qbits; /* size of q in bits (default: 160) */ | 74 | int qbits; /* size of q in bits (default: 160) */ |
@@ -78,11 +77,13 @@ typedef struct | |||
78 | int gentmp[2]; | 77 | int gentmp[2]; |
79 | /* message digest */ | 78 | /* message digest */ |
80 | const EVP_MD *md; /* MD for the signature */ | 79 | const EVP_MD *md; /* MD for the signature */ |
81 | } DSA_PKEY_CTX; | 80 | } DSA_PKEY_CTX; |
82 | 81 | ||
83 | static int pkey_dsa_init(EVP_PKEY_CTX *ctx) | 82 | static int |
84 | { | 83 | pkey_dsa_init(EVP_PKEY_CTX *ctx) |
84 | { | ||
85 | DSA_PKEY_CTX *dctx; | 85 | DSA_PKEY_CTX *dctx; |
86 | |||
86 | dctx = malloc(sizeof(DSA_PKEY_CTX)); | 87 | dctx = malloc(sizeof(DSA_PKEY_CTX)); |
87 | if (!dctx) | 88 | if (!dctx) |
88 | return 0; | 89 | return 0; |
@@ -96,11 +97,13 @@ static int pkey_dsa_init(EVP_PKEY_CTX *ctx) | |||
96 | ctx->keygen_info_count = 2; | 97 | ctx->keygen_info_count = 2; |
97 | 98 | ||
98 | return 1; | 99 | return 1; |
99 | } | 100 | } |
100 | 101 | ||
101 | static int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) | 102 | static int |
102 | { | 103 | pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
104 | { | ||
103 | DSA_PKEY_CTX *dctx, *sctx; | 105 | DSA_PKEY_CTX *dctx, *sctx; |
106 | |||
104 | if (!pkey_dsa_init(dst)) | 107 | if (!pkey_dsa_init(dst)) |
105 | return 0; | 108 | return 0; |
106 | sctx = src->data; | 109 | sctx = src->data; |
@@ -110,17 +113,20 @@ static int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) | |||
110 | dctx->pmd = sctx->pmd; | 113 | dctx->pmd = sctx->pmd; |
111 | dctx->md = sctx->md; | 114 | dctx->md = sctx->md; |
112 | return 1; | 115 | return 1; |
113 | } | 116 | } |
114 | 117 | ||
115 | static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx) | 118 | static void |
116 | { | 119 | pkey_dsa_cleanup(EVP_PKEY_CTX *ctx) |
120 | { | ||
117 | DSA_PKEY_CTX *dctx = ctx->data; | 121 | DSA_PKEY_CTX *dctx = ctx->data; |
122 | |||
118 | free(dctx); | 123 | free(dctx); |
119 | } | 124 | } |
120 | 125 | ||
121 | static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | 126 | static int |
122 | const unsigned char *tbs, size_t tbslen) | 127 | pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, |
123 | { | 128 | const unsigned char *tbs, size_t tbslen) |
129 | { | ||
124 | int ret, type; | 130 | int ret, type; |
125 | unsigned int sltmp; | 131 | unsigned int sltmp; |
126 | DSA_PKEY_CTX *dctx = ctx->data; | 132 | DSA_PKEY_CTX *dctx = ctx->data; |
@@ -137,12 +143,12 @@ static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | |||
137 | return ret; | 143 | return ret; |
138 | *siglen = sltmp; | 144 | *siglen = sltmp; |
139 | return 1; | 145 | return 1; |
140 | } | 146 | } |
141 | 147 | ||
142 | static int pkey_dsa_verify(EVP_PKEY_CTX *ctx, | 148 | static int |
143 | const unsigned char *sig, size_t siglen, | 149 | pkey_dsa_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen, |
144 | const unsigned char *tbs, size_t tbslen) | 150 | const unsigned char *tbs, size_t tbslen) |
145 | { | 151 | { |
146 | int ret, type; | 152 | int ret, type; |
147 | DSA_PKEY_CTX *dctx = ctx->data; | 153 | DSA_PKEY_CTX *dctx = ctx->data; |
148 | DSA *dsa = ctx->pkey->pkey.dsa; | 154 | DSA *dsa = ctx->pkey->pkey.dsa; |
@@ -155,69 +161,67 @@ static int pkey_dsa_verify(EVP_PKEY_CTX *ctx, | |||
155 | ret = DSA_verify(type, tbs, tbslen, sig, siglen, dsa); | 161 | ret = DSA_verify(type, tbs, tbslen, sig, siglen, dsa); |
156 | 162 | ||
157 | return ret; | 163 | return ret; |
158 | } | 164 | } |
159 | 165 | ||
160 | static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | 166 | static int |
161 | { | 167 | pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
168 | { | ||
162 | DSA_PKEY_CTX *dctx = ctx->data; | 169 | DSA_PKEY_CTX *dctx = ctx->data; |
163 | switch (type) | 170 | |
164 | { | 171 | switch (type) { |
165 | case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS: | 172 | case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS: |
166 | if (p1 < 256) | 173 | if (p1 < 256) |
167 | return -2; | 174 | return -2; |
168 | dctx->nbits = p1; | 175 | dctx->nbits = p1; |
169 | return 1; | 176 | return 1; |
170 | 177 | ||
171 | case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS: | 178 | case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS: |
172 | if (p1 != 160 && p1 != 224 && p1 && p1 != 256) | 179 | if (p1 != 160 && p1 != 224 && p1 && p1 != 256) |
173 | return -2; | 180 | return -2; |
174 | dctx->qbits = p1; | 181 | dctx->qbits = p1; |
175 | return 1; | 182 | return 1; |
176 | 183 | ||
177 | case EVP_PKEY_CTRL_DSA_PARAMGEN_MD: | 184 | case EVP_PKEY_CTRL_DSA_PARAMGEN_MD: |
178 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && | 185 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && |
179 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && | 186 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && |
180 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256) | 187 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256) { |
181 | { | ||
182 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); | 188 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); |
183 | return 0; | 189 | return 0; |
184 | } | 190 | } |
185 | dctx->md = p2; | 191 | dctx->md = p2; |
186 | return 1; | 192 | return 1; |
187 | 193 | ||
188 | case EVP_PKEY_CTRL_MD: | 194 | case EVP_PKEY_CTRL_MD: |
189 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && | 195 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && |
190 | EVP_MD_type((const EVP_MD *)p2) != NID_dsa && | 196 | EVP_MD_type((const EVP_MD *)p2) != NID_dsa && |
191 | EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA && | 197 | EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA && |
192 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && | 198 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && |
193 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256 && | 199 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256 && |
194 | EVP_MD_type((const EVP_MD *)p2) != NID_sha384 && | 200 | EVP_MD_type((const EVP_MD *)p2) != NID_sha384 && |
195 | EVP_MD_type((const EVP_MD *)p2) != NID_sha512) | 201 | EVP_MD_type((const EVP_MD *)p2) != NID_sha512) { |
196 | { | ||
197 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); | 202 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); |
198 | return 0; | 203 | return 0; |
199 | } | 204 | } |
200 | dctx->md = p2; | 205 | dctx->md = p2; |
201 | return 1; | 206 | return 1; |
202 | 207 | ||
203 | case EVP_PKEY_CTRL_DIGESTINIT: | 208 | case EVP_PKEY_CTRL_DIGESTINIT: |
204 | case EVP_PKEY_CTRL_PKCS7_SIGN: | 209 | case EVP_PKEY_CTRL_PKCS7_SIGN: |
205 | case EVP_PKEY_CTRL_CMS_SIGN: | 210 | case EVP_PKEY_CTRL_CMS_SIGN: |
206 | return 1; | 211 | return 1; |
207 | 212 | ||
208 | case EVP_PKEY_CTRL_PEER_KEY: | 213 | case EVP_PKEY_CTRL_PEER_KEY: |
209 | DSAerr(DSA_F_PKEY_DSA_CTRL, | 214 | DSAerr(DSA_F_PKEY_DSA_CTRL, |
210 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 215 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
211 | return -2; | 216 | return -2; |
212 | default: | 217 | default: |
213 | return -2; | 218 | return -2; |
214 | |||
215 | } | ||
216 | } | 219 | } |
220 | } | ||
217 | 221 | ||
218 | static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx, | 222 | static int |
219 | const char *type, const char *value) | 223 | pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value) |
220 | { | 224 | { |
221 | long lval; | 225 | long lval; |
222 | char *ep; | 226 | char *ep; |
223 | 227 | ||
@@ -228,69 +232,71 @@ static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx, | |||
228 | lval = strtol(value, &ep, 10); | 232 | lval = strtol(value, &ep, 10); |
229 | if (value[0] == '\0' || *ep != '\0') | 233 | if (value[0] == '\0' || *ep != '\0') |
230 | goto not_a_number; | 234 | goto not_a_number; |
231 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | 235 | if ((errno == ERANGE && |
236 | (lval == LONG_MAX || lval == LONG_MIN)) || | ||
232 | (lval > INT_MAX || lval < INT_MIN)) | 237 | (lval > INT_MAX || lval < INT_MIN)) |
233 | goto out_of_range; | 238 | goto out_of_range; |
234 | nbits = lval; | 239 | nbits = lval; |
235 | return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); | 240 | return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); |
236 | } | 241 | } else if (!strcmp(type, "dsa_paramgen_q_bits")) { |
237 | if (!strcmp(type, "dsa_paramgen_q_bits")) { | ||
238 | int qbits; | 242 | int qbits; |
239 | 243 | ||
240 | errno = 0; | 244 | errno = 0; |
241 | lval = strtol(value, &ep, 10); | 245 | lval = strtol(value, &ep, 10); |
242 | if (value[0] == '\0' || *ep != '\0') | 246 | if (value[0] == '\0' || *ep != '\0') |
243 | goto not_a_number; | 247 | goto not_a_number; |
244 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | 248 | if ((errno == ERANGE && |
249 | (lval == LONG_MAX || lval == LONG_MIN)) || | ||
245 | (lval > INT_MAX || lval < INT_MIN)) | 250 | (lval > INT_MAX || lval < INT_MIN)) |
246 | goto out_of_range; | 251 | goto out_of_range; |
247 | qbits = lval; | 252 | qbits = lval; |
248 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN, | 253 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, |
249 | EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits, NULL); | 254 | EVP_PKEY_OP_PARAMGEN, EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, |
250 | } | 255 | qbits, NULL); |
251 | if (!strcmp(type, "dsa_paramgen_md")){ | 256 | } else if (!strcmp(type, "dsa_paramgen_md")) { |
252 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN, | 257 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, |
253 | EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, | 258 | EVP_PKEY_OP_PARAMGEN, EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, |
254 | (void *)EVP_get_digestbyname(value)); | 259 | (void *)EVP_get_digestbyname(value)); |
255 | } | 260 | } |
256 | not_a_number: | 261 | not_a_number: |
257 | out_of_range: | 262 | out_of_range: |
258 | return -2; | 263 | return -2; |
259 | } | 264 | } |
260 | 265 | ||
261 | static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | 266 | static int |
262 | { | 267 | pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
268 | { | ||
263 | DSA *dsa = NULL; | 269 | DSA *dsa = NULL; |
264 | DSA_PKEY_CTX *dctx = ctx->data; | 270 | DSA_PKEY_CTX *dctx = ctx->data; |
265 | BN_GENCB *pcb, cb; | 271 | BN_GENCB *pcb, cb; |
266 | int ret; | 272 | int ret; |
267 | if (ctx->pkey_gencb) | 273 | |
268 | { | 274 | if (ctx->pkey_gencb) { |
269 | pcb = &cb; | 275 | pcb = &cb; |
270 | evp_pkey_set_cb_translate(pcb, ctx); | 276 | evp_pkey_set_cb_translate(pcb, ctx); |
271 | } | 277 | } else |
272 | else | ||
273 | pcb = NULL; | 278 | pcb = NULL; |
274 | dsa = DSA_new(); | 279 | dsa = DSA_new(); |
275 | if (!dsa) | 280 | if (!dsa) |
276 | return 0; | 281 | return 0; |
277 | ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd, | 282 | ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd, |
278 | NULL, 0, NULL, NULL, NULL, pcb); | 283 | NULL, 0, NULL, NULL, NULL, pcb); |
279 | if (ret) | 284 | if (ret) |
280 | EVP_PKEY_assign_DSA(pkey, dsa); | 285 | EVP_PKEY_assign_DSA(pkey, dsa); |
281 | else | 286 | else |
282 | DSA_free(dsa); | 287 | DSA_free(dsa); |
283 | return ret; | 288 | return ret; |
284 | } | 289 | } |
285 | 290 | ||
286 | static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | 291 | static int |
287 | { | 292 | pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
293 | { | ||
288 | DSA *dsa = NULL; | 294 | DSA *dsa = NULL; |
289 | if (ctx->pkey == NULL) | 295 | |
290 | { | 296 | if (ctx->pkey == NULL) { |
291 | DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET); | 297 | DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET); |
292 | return 0; | 298 | return 0; |
293 | } | 299 | } |
294 | dsa = DSA_new(); | 300 | dsa = DSA_new(); |
295 | if (!dsa) | 301 | if (!dsa) |
296 | return 0; | 302 | return 0; |
@@ -299,7 +305,7 @@ static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | |||
299 | if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) | 305 | if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) |
300 | return 0; | 306 | return 0; |
301 | return DSA_generate_key(pkey->pkey.dsa); | 307 | return DSA_generate_key(pkey->pkey.dsa); |
302 | } | 308 | } |
303 | 309 | ||
304 | const EVP_PKEY_METHOD dsa_pkey_meth = { | 310 | const EVP_PKEY_METHOD dsa_pkey_meth = { |
305 | .pkey_id = EVP_PKEY_DSA, | 311 | .pkey_id = EVP_PKEY_DSA, |