]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
3 | * 2006. | |
448be743 DSH |
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 | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
448be743 DSH |
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 <stdio.h> | |
60 | #include "cryptlib.h" | |
61 | #include <openssl/x509.h> | |
62 | #include <openssl/asn1.h> | |
63 | #include <openssl/dsa.h> | |
1e26a8ba | 64 | #include <openssl/bn.h> |
8931b30d | 65 | #ifndef OPENSSL_NO_CMS |
0f113f3e | 66 | # include <openssl/cms.h> |
8931b30d | 67 | #endif |
5fe736e5 | 68 | #include "internal/asn1_int.h" |
448be743 DSH |
69 | |
70 | static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | |
0f113f3e MC |
71 | { |
72 | const unsigned char *p, *pm; | |
73 | int pklen, pmlen; | |
74 | int ptype; | |
75 | void *pval; | |
76 | ASN1_STRING *pstr; | |
77 | X509_ALGOR *palg; | |
78 | ASN1_INTEGER *public_key = NULL; | |
79 | ||
80 | DSA *dsa = NULL; | |
81 | ||
82 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | |
83 | return 0; | |
84 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
85 | ||
86 | if (ptype == V_ASN1_SEQUENCE) { | |
87 | pstr = pval; | |
88 | pm = pstr->data; | |
89 | pmlen = pstr->length; | |
90 | ||
91 | if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) { | |
92 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); | |
93 | goto err; | |
94 | } | |
95 | ||
96 | } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) { | |
97 | if (!(dsa = DSA_new())) { | |
98 | DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE); | |
99 | goto err; | |
100 | } | |
101 | } else { | |
102 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR); | |
103 | goto err; | |
104 | } | |
105 | ||
106 | if (!(public_key = d2i_ASN1_INTEGER(NULL, &p, pklen))) { | |
107 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); | |
108 | goto err; | |
109 | } | |
110 | ||
111 | if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) { | |
112 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR); | |
113 | goto err; | |
114 | } | |
115 | ||
116 | ASN1_INTEGER_free(public_key); | |
117 | EVP_PKEY_assign_DSA(pkey, dsa); | |
118 | return 1; | |
119 | ||
120 | err: | |
121 | if (public_key) | |
122 | ASN1_INTEGER_free(public_key); | |
d6407083 | 123 | DSA_free(dsa); |
0f113f3e MC |
124 | return 0; |
125 | ||
126 | } | |
448be743 | 127 | |
6f81892e | 128 | static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
0f113f3e MC |
129 | { |
130 | DSA *dsa; | |
0f113f3e MC |
131 | int ptype; |
132 | unsigned char *penc = NULL; | |
133 | int penclen; | |
0c7ca403 | 134 | ASN1_STRING *str = NULL; |
ea6b07b5 | 135 | ASN1_INTEGER *pubint = NULL; |
0f113f3e MC |
136 | |
137 | dsa = pkey->pkey.dsa; | |
138 | if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) { | |
0f113f3e | 139 | str = ASN1_STRING_new(); |
0c7ca403 MC |
140 | if (!str) { |
141 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | |
142 | goto err; | |
143 | } | |
0f113f3e MC |
144 | str->length = i2d_DSAparams(dsa, &str->data); |
145 | if (str->length <= 0) { | |
146 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | |
147 | goto err; | |
148 | } | |
0f113f3e MC |
149 | ptype = V_ASN1_SEQUENCE; |
150 | } else | |
151 | ptype = V_ASN1_UNDEF; | |
152 | ||
ea6b07b5 | 153 | pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL); |
0f113f3e | 154 | |
ea6b07b5 DSH |
155 | if (pubint == NULL) { |
156 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | |
157 | goto err; | |
158 | } | |
159 | ||
160 | penclen = i2d_ASN1_INTEGER(pubint, &penc); | |
161 | ASN1_INTEGER_free(pubint); | |
0f113f3e MC |
162 | |
163 | if (penclen <= 0) { | |
164 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | |
165 | goto err; | |
166 | } | |
167 | ||
168 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA), | |
0c7ca403 | 169 | ptype, str, penc, penclen)) |
0f113f3e MC |
170 | return 1; |
171 | ||
172 | err: | |
173 | if (penc) | |
174 | OPENSSL_free(penc); | |
0dfb9398 | 175 | ASN1_STRING_free(str); |
0f113f3e MC |
176 | |
177 | return 0; | |
178 | } | |
179 | ||
180 | /* | |
181 | * In PKCS#8 DSA: you just get a private key integer and parameters in the | |
448be743 DSH |
182 | * AlgorithmIdentifier the pubkey must be recalculated. |
183 | */ | |
0f113f3e | 184 | |
448be743 | 185 | static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) |
0f113f3e MC |
186 | { |
187 | const unsigned char *p, *pm; | |
188 | int pklen, pmlen; | |
189 | int ptype; | |
190 | void *pval; | |
191 | ASN1_STRING *pstr; | |
192 | X509_ALGOR *palg; | |
193 | ASN1_INTEGER *privkey = NULL; | |
194 | BN_CTX *ctx = NULL; | |
195 | ||
196 | STACK_OF(ASN1_TYPE) *ndsa = NULL; | |
197 | DSA *dsa = NULL; | |
198 | ||
199 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) | |
200 | return 0; | |
201 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
202 | ||
203 | /* Check for broken DSA PKCS#8, UGH! */ | |
204 | if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) { | |
205 | ASN1_TYPE *t1, *t2; | |
206 | if (!(ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen))) | |
207 | goto decerr; | |
208 | if (sk_ASN1_TYPE_num(ndsa) != 2) | |
209 | goto decerr; | |
50e735f9 MC |
210 | /*- |
211 | * Handle Two broken types: | |
212 | * SEQUENCE {parameters, priv_key} | |
213 | * SEQUENCE {pub_key, priv_key} | |
214 | */ | |
0f113f3e MC |
215 | |
216 | t1 = sk_ASN1_TYPE_value(ndsa, 0); | |
217 | t2 = sk_ASN1_TYPE_value(ndsa, 1); | |
218 | if (t1->type == V_ASN1_SEQUENCE) { | |
219 | p8->broken = PKCS8_EMBEDDED_PARAM; | |
220 | pval = t1->value.ptr; | |
221 | } else if (ptype == V_ASN1_SEQUENCE) | |
222 | p8->broken = PKCS8_NS_DB; | |
223 | else | |
224 | goto decerr; | |
225 | ||
226 | if (t2->type != V_ASN1_INTEGER) | |
227 | goto decerr; | |
228 | ||
229 | privkey = t2->value.integer; | |
230 | } else { | |
231 | const unsigned char *q = p; | |
232 | if (!(privkey = d2i_ASN1_INTEGER(NULL, &p, pklen))) | |
233 | goto decerr; | |
234 | if (privkey->type == V_ASN1_NEG_INTEGER) { | |
235 | p8->broken = PKCS8_NEG_PRIVKEY; | |
a8ae0891 | 236 | ASN1_STRING_clear_free(privkey); |
0f113f3e MC |
237 | if (!(privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen))) |
238 | goto decerr; | |
239 | } | |
240 | if (ptype != V_ASN1_SEQUENCE) | |
241 | goto decerr; | |
242 | } | |
243 | ||
244 | pstr = pval; | |
245 | pm = pstr->data; | |
246 | pmlen = pstr->length; | |
247 | if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) | |
248 | goto decerr; | |
249 | /* We have parameters now set private key */ | |
250 | if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) { | |
251 | DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR); | |
252 | goto dsaerr; | |
253 | } | |
254 | /* Calculate public key */ | |
255 | if (!(dsa->pub_key = BN_new())) { | |
256 | DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); | |
257 | goto dsaerr; | |
258 | } | |
259 | if (!(ctx = BN_CTX_new())) { | |
260 | DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); | |
261 | goto dsaerr; | |
262 | } | |
263 | ||
264 | if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { | |
265 | DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR); | |
266 | goto dsaerr; | |
267 | } | |
268 | ||
269 | EVP_PKEY_assign_DSA(pkey, dsa); | |
270 | BN_CTX_free(ctx); | |
271 | if (ndsa) | |
272 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); | |
273 | else | |
a8ae0891 | 274 | ASN1_STRING_clear_free(privkey); |
0f113f3e MC |
275 | |
276 | return 1; | |
277 | ||
278 | decerr: | |
279 | DSAerr(DSA_F_DSA_PRIV_DECODE, EVP_R_DECODE_ERROR); | |
280 | dsaerr: | |
281 | BN_CTX_free(ctx); | |
282 | if (privkey) | |
a8ae0891 | 283 | ASN1_STRING_clear_free(privkey); |
0f113f3e MC |
284 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); |
285 | DSA_free(dsa); | |
286 | return 0; | |
287 | } | |
448be743 | 288 | |
6f81892e | 289 | static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) |
448be743 | 290 | { |
0f113f3e MC |
291 | ASN1_STRING *params = NULL; |
292 | ASN1_INTEGER *prkey = NULL; | |
293 | unsigned char *dp = NULL; | |
294 | int dplen; | |
295 | ||
296 | if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) { | |
297 | DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS); | |
298 | goto err; | |
299 | } | |
300 | ||
301 | params = ASN1_STRING_new(); | |
302 | ||
303 | if (!params) { | |
304 | DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); | |
305 | goto err; | |
306 | } | |
307 | ||
308 | params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data); | |
309 | if (params->length <= 0) { | |
310 | DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); | |
311 | goto err; | |
312 | } | |
313 | params->type = V_ASN1_SEQUENCE; | |
314 | ||
315 | /* Get private key into integer */ | |
316 | prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL); | |
317 | ||
318 | if (!prkey) { | |
319 | DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR); | |
320 | goto err; | |
321 | } | |
322 | ||
323 | dplen = i2d_ASN1_INTEGER(prkey, &dp); | |
324 | ||
a8ae0891 | 325 | ASN1_STRING_clear_free(prkey); |
0f113f3e MC |
326 | |
327 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0, | |
328 | V_ASN1_SEQUENCE, params, dp, dplen)) | |
329 | goto err; | |
330 | ||
331 | return 1; | |
332 | ||
333 | err: | |
334 | if (dp != NULL) | |
335 | OPENSSL_free(dp); | |
0dfb9398 | 336 | ASN1_STRING_free(params); |
0f113f3e | 337 | if (prkey != NULL) |
a8ae0891 | 338 | ASN1_STRING_clear_free(prkey); |
0f113f3e | 339 | return 0; |
448be743 DSH |
340 | } |
341 | ||
6f81892e | 342 | static int int_dsa_size(const EVP_PKEY *pkey) |
0f113f3e MC |
343 | { |
344 | return (DSA_size(pkey->pkey.dsa)); | |
345 | } | |
6f81892e DSH |
346 | |
347 | static int dsa_bits(const EVP_PKEY *pkey) | |
0f113f3e MC |
348 | { |
349 | return BN_num_bits(pkey->pkey.dsa->p); | |
350 | } | |
6f81892e | 351 | |
2514fa79 | 352 | static int dsa_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
353 | { |
354 | return DSA_security_bits(pkey->pkey.dsa); | |
355 | } | |
2514fa79 | 356 | |
6f81892e | 357 | static int dsa_missing_parameters(const EVP_PKEY *pkey) |
0f113f3e MC |
358 | { |
359 | DSA *dsa; | |
360 | dsa = pkey->pkey.dsa; | |
361 | if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL)) | |
362 | return 1; | |
363 | return 0; | |
364 | } | |
6f81892e DSH |
365 | |
366 | static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) | |
0f113f3e MC |
367 | { |
368 | BIGNUM *a; | |
369 | ||
370 | if ((a = BN_dup(from->pkey.dsa->p)) == NULL) | |
371 | return 0; | |
372 | if (to->pkey.dsa->p != NULL) | |
373 | BN_free(to->pkey.dsa->p); | |
374 | to->pkey.dsa->p = a; | |
375 | ||
376 | if ((a = BN_dup(from->pkey.dsa->q)) == NULL) | |
377 | return 0; | |
378 | if (to->pkey.dsa->q != NULL) | |
379 | BN_free(to->pkey.dsa->q); | |
380 | to->pkey.dsa->q = a; | |
381 | ||
382 | if ((a = BN_dup(from->pkey.dsa->g)) == NULL) | |
383 | return 0; | |
384 | if (to->pkey.dsa->g != NULL) | |
385 | BN_free(to->pkey.dsa->g); | |
386 | to->pkey.dsa->g = a; | |
387 | return 1; | |
388 | } | |
6f81892e DSH |
389 | |
390 | static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) | |
0f113f3e MC |
391 | { |
392 | if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) || | |
393 | BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) || | |
394 | BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g)) | |
395 | return 0; | |
396 | else | |
397 | return 1; | |
398 | } | |
6f81892e | 399 | |
0cb8499b | 400 | static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
401 | { |
402 | if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0) | |
403 | return 0; | |
404 | else | |
405 | return 1; | |
406 | } | |
0cb8499b | 407 | |
6f81892e | 408 | static void int_dsa_free(EVP_PKEY *pkey) |
0f113f3e MC |
409 | { |
410 | DSA_free(pkey->pkey.dsa); | |
411 | } | |
6f81892e | 412 | |
35208f36 | 413 | static void update_buflen(const BIGNUM *b, size_t *pbuflen) |
0f113f3e MC |
414 | { |
415 | size_t i; | |
416 | if (!b) | |
417 | return; | |
418 | if (*pbuflen < (i = (size_t)BN_num_bytes(b))) | |
419 | *pbuflen = i; | |
420 | } | |
35208f36 | 421 | |
777c47ac | 422 | static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) |
0f113f3e MC |
423 | { |
424 | unsigned char *m = NULL; | |
425 | int ret = 0; | |
426 | size_t buf_len = 0; | |
427 | const char *ktype = NULL; | |
428 | ||
429 | const BIGNUM *priv_key, *pub_key; | |
430 | ||
431 | if (ptype == 2) | |
432 | priv_key = x->priv_key; | |
433 | else | |
434 | priv_key = NULL; | |
435 | ||
436 | if (ptype > 0) | |
437 | pub_key = x->pub_key; | |
438 | else | |
439 | pub_key = NULL; | |
440 | ||
441 | if (ptype == 2) | |
442 | ktype = "Private-Key"; | |
443 | else if (ptype == 1) | |
444 | ktype = "Public-Key"; | |
445 | else | |
446 | ktype = "DSA-Parameters"; | |
447 | ||
448 | update_buflen(x->p, &buf_len); | |
449 | update_buflen(x->q, &buf_len); | |
450 | update_buflen(x->g, &buf_len); | |
451 | update_buflen(priv_key, &buf_len); | |
452 | update_buflen(pub_key, &buf_len); | |
453 | ||
b196e7d9 | 454 | m = OPENSSL_malloc(buf_len + 10); |
0f113f3e MC |
455 | if (m == NULL) { |
456 | DSAerr(DSA_F_DO_DSA_PRINT, ERR_R_MALLOC_FAILURE); | |
457 | goto err; | |
458 | } | |
459 | ||
460 | if (priv_key) { | |
461 | if (!BIO_indent(bp, off, 128)) | |
462 | goto err; | |
463 | if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) | |
464 | <= 0) | |
465 | goto err; | |
466 | } | |
467 | ||
468 | if (!ASN1_bn_print(bp, "priv:", priv_key, m, off)) | |
469 | goto err; | |
470 | if (!ASN1_bn_print(bp, "pub: ", pub_key, m, off)) | |
471 | goto err; | |
472 | if (!ASN1_bn_print(bp, "P: ", x->p, m, off)) | |
473 | goto err; | |
474 | if (!ASN1_bn_print(bp, "Q: ", x->q, m, off)) | |
475 | goto err; | |
476 | if (!ASN1_bn_print(bp, "G: ", x->g, m, off)) | |
477 | goto err; | |
478 | ret = 1; | |
479 | err: | |
480 | if (m != NULL) | |
481 | OPENSSL_free(m); | |
482 | return (ret); | |
483 | } | |
35208f36 | 484 | |
3e4585c8 | 485 | static int dsa_param_decode(EVP_PKEY *pkey, |
0f113f3e MC |
486 | const unsigned char **pder, int derlen) |
487 | { | |
488 | DSA *dsa; | |
489 | if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) { | |
490 | DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB); | |
491 | return 0; | |
492 | } | |
493 | EVP_PKEY_assign_DSA(pkey, dsa); | |
494 | return 1; | |
495 | } | |
3e4585c8 DSH |
496 | |
497 | static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
498 | { |
499 | return i2d_DSAparams(pkey->pkey.dsa, pder); | |
500 | } | |
35208f36 DSH |
501 | |
502 | static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
503 | ASN1_PCTX *ctx) |
504 | { | |
505 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 0); | |
506 | } | |
35208f36 DSH |
507 | |
508 | static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
509 | ASN1_PCTX *ctx) |
510 | { | |
511 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 1); | |
512 | } | |
35208f36 DSH |
513 | |
514 | static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
515 | ASN1_PCTX *ctx) |
516 | { | |
517 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 2); | |
518 | } | |
35208f36 | 519 | |
e4263314 | 520 | static int old_dsa_priv_decode(EVP_PKEY *pkey, |
0f113f3e MC |
521 | const unsigned char **pder, int derlen) |
522 | { | |
523 | DSA *dsa; | |
524 | if (!(dsa = d2i_DSAPrivateKey(NULL, pder, derlen))) { | |
525 | DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB); | |
526 | return 0; | |
527 | } | |
528 | EVP_PKEY_assign_DSA(pkey, dsa); | |
529 | return 1; | |
530 | } | |
e4263314 DSH |
531 | |
532 | static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
533 | { |
534 | return i2d_DSAPrivateKey(pkey->pkey.dsa, pder); | |
535 | } | |
e4263314 | 536 | |
fa1ba589 | 537 | static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, |
0f113f3e MC |
538 | const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) |
539 | { | |
540 | DSA_SIG *dsa_sig; | |
541 | const unsigned char *p; | |
542 | if (!sig) { | |
543 | if (BIO_puts(bp, "\n") <= 0) | |
544 | return 0; | |
545 | else | |
546 | return 1; | |
547 | } | |
548 | p = sig->data; | |
549 | dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length); | |
550 | if (dsa_sig) { | |
551 | int rv = 0; | |
552 | size_t buf_len = 0; | |
553 | unsigned char *m = NULL; | |
554 | update_buflen(dsa_sig->r, &buf_len); | |
555 | update_buflen(dsa_sig->s, &buf_len); | |
556 | m = OPENSSL_malloc(buf_len + 10); | |
557 | if (m == NULL) { | |
558 | DSAerr(DSA_F_DSA_SIG_PRINT, ERR_R_MALLOC_FAILURE); | |
559 | goto err; | |
560 | } | |
561 | ||
562 | if (BIO_write(bp, "\n", 1) != 1) | |
563 | goto err; | |
564 | ||
565 | if (!ASN1_bn_print(bp, "r: ", dsa_sig->r, m, indent)) | |
566 | goto err; | |
567 | if (!ASN1_bn_print(bp, "s: ", dsa_sig->s, m, indent)) | |
568 | goto err; | |
569 | rv = 1; | |
570 | err: | |
571 | if (m) | |
572 | OPENSSL_free(m); | |
573 | DSA_SIG_free(dsa_sig); | |
574 | return rv; | |
575 | } | |
576 | return X509_signature_dump(bp, sig, indent); | |
577 | } | |
fa1ba589 | 578 | |
492a9e24 | 579 | static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
0f113f3e MC |
580 | { |
581 | switch (op) { | |
582 | case ASN1_PKEY_CTRL_PKCS7_SIGN: | |
583 | if (arg1 == 0) { | |
584 | int snid, hnid; | |
585 | X509_ALGOR *alg1, *alg2; | |
586 | PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2); | |
587 | if (alg1 == NULL || alg1->algorithm == NULL) | |
588 | return -1; | |
589 | hnid = OBJ_obj2nid(alg1->algorithm); | |
590 | if (hnid == NID_undef) | |
591 | return -1; | |
592 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | |
593 | return -1; | |
594 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | |
595 | } | |
596 | return 1; | |
8931b30d | 597 | #ifndef OPENSSL_NO_CMS |
0f113f3e MC |
598 | case ASN1_PKEY_CTRL_CMS_SIGN: |
599 | if (arg1 == 0) { | |
600 | int snid, hnid; | |
601 | X509_ALGOR *alg1, *alg2; | |
602 | CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2); | |
603 | if (alg1 == NULL || alg1->algorithm == NULL) | |
604 | return -1; | |
605 | hnid = OBJ_obj2nid(alg1->algorithm); | |
606 | if (hnid == NID_undef) | |
607 | return -1; | |
608 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | |
609 | return -1; | |
610 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | |
611 | } | |
612 | return 1; | |
613 | ||
614 | case ASN1_PKEY_CTRL_CMS_RI_TYPE: | |
615 | *(int *)arg2 = CMS_RECIPINFO_NONE; | |
616 | return 1; | |
8931b30d | 617 | #endif |
492a9e24 | 618 | |
0f113f3e MC |
619 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
620 | *(int *)arg2 = NID_sha256; | |
621 | return 2; | |
03919683 | 622 | |
0f113f3e MC |
623 | default: |
624 | return -2; | |
492a9e24 | 625 | |
0f113f3e | 626 | } |
492a9e24 | 627 | |
0f113f3e | 628 | } |
492a9e24 | 629 | |
448be743 DSH |
630 | /* NB these are sorted in pkey_id order, lowest first */ |
631 | ||
0f113f3e MC |
632 | const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = { |
633 | ||
634 | { | |
635 | EVP_PKEY_DSA2, | |
636 | EVP_PKEY_DSA, | |
637 | ASN1_PKEY_ALIAS}, | |
638 | ||
639 | { | |
640 | EVP_PKEY_DSA1, | |
641 | EVP_PKEY_DSA, | |
642 | ASN1_PKEY_ALIAS}, | |
643 | ||
644 | { | |
645 | EVP_PKEY_DSA4, | |
646 | EVP_PKEY_DSA, | |
647 | ASN1_PKEY_ALIAS}, | |
648 | ||
649 | { | |
650 | EVP_PKEY_DSA3, | |
651 | EVP_PKEY_DSA, | |
652 | ASN1_PKEY_ALIAS}, | |
653 | ||
654 | { | |
655 | EVP_PKEY_DSA, | |
656 | EVP_PKEY_DSA, | |
657 | 0, | |
658 | ||
659 | "DSA", | |
660 | "OpenSSL DSA method", | |
661 | ||
662 | dsa_pub_decode, | |
663 | dsa_pub_encode, | |
664 | dsa_pub_cmp, | |
665 | dsa_pub_print, | |
666 | ||
667 | dsa_priv_decode, | |
668 | dsa_priv_encode, | |
669 | dsa_priv_print, | |
670 | ||
671 | int_dsa_size, | |
672 | dsa_bits, | |
673 | dsa_security_bits, | |
674 | ||
675 | dsa_param_decode, | |
676 | dsa_param_encode, | |
677 | dsa_missing_parameters, | |
678 | dsa_copy_parameters, | |
679 | dsa_cmp_parameters, | |
680 | dsa_param_print, | |
681 | dsa_sig_print, | |
682 | ||
683 | int_dsa_free, | |
684 | dsa_pkey_ctrl, | |
685 | old_dsa_priv_decode, | |
686 | old_dsa_priv_encode} | |
687 | }; |