]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
d2e9e320 | 2 | * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved. |
448be743 | 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 | |
448be743 DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
448be743 DSH |
11 | #include <openssl/x509.h> |
12 | #include <openssl/asn1.h> | |
1e26a8ba | 13 | #include <openssl/bn.h> |
3c27208f | 14 | #include <openssl/cms.h> |
4889dadc MC |
15 | #include <openssl/core_names.h> |
16 | #include "internal/cryptlib.h" | |
25f2138b DMSP |
17 | #include "crypto/asn1.h" |
18 | #include "crypto/evp.h" | |
4889dadc | 19 | #include "internal/param_build.h" |
706457b7 | 20 | #include "dsa_local.h" |
448be743 DSH |
21 | |
22 | static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | |
0f113f3e MC |
23 | { |
24 | const unsigned char *p, *pm; | |
25 | int pklen, pmlen; | |
26 | int ptype; | |
ac4e2577 DSH |
27 | const void *pval; |
28 | const ASN1_STRING *pstr; | |
0f113f3e MC |
29 | X509_ALGOR *palg; |
30 | ASN1_INTEGER *public_key = NULL; | |
31 | ||
32 | DSA *dsa = NULL; | |
33 | ||
34 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | |
35 | return 0; | |
36 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
37 | ||
38 | if (ptype == V_ASN1_SEQUENCE) { | |
39 | pstr = pval; | |
40 | pm = pstr->data; | |
41 | pmlen = pstr->length; | |
42 | ||
75ebbd9a | 43 | if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) { |
0f113f3e MC |
44 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); |
45 | goto err; | |
46 | } | |
47 | ||
48 | } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) { | |
75ebbd9a | 49 | if ((dsa = DSA_new()) == NULL) { |
0f113f3e MC |
50 | DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE); |
51 | goto err; | |
52 | } | |
53 | } else { | |
54 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR); | |
55 | goto err; | |
56 | } | |
57 | ||
75ebbd9a | 58 | if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) { |
0f113f3e MC |
59 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); |
60 | goto err; | |
61 | } | |
62 | ||
75ebbd9a | 63 | if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) { |
0f113f3e MC |
64 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR); |
65 | goto err; | |
66 | } | |
67 | ||
4889dadc | 68 | dsa->dirty_cnt++; |
0f113f3e MC |
69 | ASN1_INTEGER_free(public_key); |
70 | EVP_PKEY_assign_DSA(pkey, dsa); | |
71 | return 1; | |
72 | ||
73 | err: | |
2ace7450 | 74 | ASN1_INTEGER_free(public_key); |
d6407083 | 75 | DSA_free(dsa); |
0f113f3e MC |
76 | return 0; |
77 | ||
78 | } | |
448be743 | 79 | |
6f81892e | 80 | static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
0f113f3e MC |
81 | { |
82 | DSA *dsa; | |
0f113f3e MC |
83 | int ptype; |
84 | unsigned char *penc = NULL; | |
85 | int penclen; | |
0c7ca403 | 86 | ASN1_STRING *str = NULL; |
ea6b07b5 | 87 | ASN1_INTEGER *pubint = NULL; |
7760384b | 88 | ASN1_OBJECT *aobj; |
0f113f3e MC |
89 | |
90 | dsa = pkey->pkey.dsa; | |
91 | if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) { | |
0f113f3e | 92 | str = ASN1_STRING_new(); |
90945fa3 | 93 | if (str == NULL) { |
0c7ca403 MC |
94 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); |
95 | goto err; | |
96 | } | |
0f113f3e MC |
97 | str->length = i2d_DSAparams(dsa, &str->data); |
98 | if (str->length <= 0) { | |
99 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | |
100 | goto err; | |
101 | } | |
0f113f3e MC |
102 | ptype = V_ASN1_SEQUENCE; |
103 | } else | |
104 | ptype = V_ASN1_UNDEF; | |
105 | ||
ea6b07b5 | 106 | pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL); |
0f113f3e | 107 | |
ea6b07b5 DSH |
108 | if (pubint == NULL) { |
109 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | |
110 | goto err; | |
111 | } | |
112 | ||
113 | penclen = i2d_ASN1_INTEGER(pubint, &penc); | |
114 | ASN1_INTEGER_free(pubint); | |
0f113f3e MC |
115 | |
116 | if (penclen <= 0) { | |
117 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | |
118 | goto err; | |
119 | } | |
120 | ||
7760384b PK |
121 | aobj = OBJ_nid2obj(EVP_PKEY_DSA); |
122 | if (aobj == NULL) | |
123 | goto err; | |
124 | ||
125 | if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen)) | |
0f113f3e MC |
126 | return 1; |
127 | ||
128 | err: | |
b548a1f1 | 129 | OPENSSL_free(penc); |
0dfb9398 | 130 | ASN1_STRING_free(str); |
0f113f3e MC |
131 | |
132 | return 0; | |
133 | } | |
134 | ||
135 | /* | |
136 | * In PKCS#8 DSA: you just get a private key integer and parameters in the | |
448be743 DSH |
137 | * AlgorithmIdentifier the pubkey must be recalculated. |
138 | */ | |
0f113f3e | 139 | |
245c6bc3 | 140 | static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) |
0f113f3e | 141 | { |
dfb10af9 | 142 | const unsigned char *p, *pm; |
0f113f3e MC |
143 | int pklen, pmlen; |
144 | int ptype; | |
ac4e2577 DSH |
145 | const void *pval; |
146 | const ASN1_STRING *pstr; | |
245c6bc3 | 147 | const X509_ALGOR *palg; |
0f113f3e MC |
148 | ASN1_INTEGER *privkey = NULL; |
149 | BN_CTX *ctx = NULL; | |
150 | ||
0f113f3e MC |
151 | DSA *dsa = NULL; |
152 | ||
ab4a81f6 DSH |
153 | int ret = 0; |
154 | ||
0f113f3e MC |
155 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) |
156 | return 0; | |
157 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
158 | ||
ab4a81f6 DSH |
159 | if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) |
160 | goto decerr; | |
dfb10af9 | 161 | if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE) |
ab4a81f6 | 162 | goto decerr; |
0f113f3e MC |
163 | |
164 | pstr = pval; | |
165 | pm = pstr->data; | |
166 | pmlen = pstr->length; | |
75ebbd9a | 167 | if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) |
0f113f3e MC |
168 | goto decerr; |
169 | /* We have parameters now set private key */ | |
74924dcb RS |
170 | if ((dsa->priv_key = BN_secure_new()) == NULL |
171 | || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) { | |
0f113f3e MC |
172 | DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR); |
173 | goto dsaerr; | |
174 | } | |
175 | /* Calculate public key */ | |
75ebbd9a | 176 | if ((dsa->pub_key = BN_new()) == NULL) { |
0f113f3e MC |
177 | DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); |
178 | goto dsaerr; | |
179 | } | |
75ebbd9a | 180 | if ((ctx = BN_CTX_new()) == NULL) { |
0f113f3e MC |
181 | DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); |
182 | goto dsaerr; | |
183 | } | |
184 | ||
6364475a | 185 | BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME); |
0f113f3e MC |
186 | if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { |
187 | DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR); | |
188 | goto dsaerr; | |
189 | } | |
190 | ||
4889dadc | 191 | dsa->dirty_cnt++; |
0f113f3e | 192 | EVP_PKEY_assign_DSA(pkey, dsa); |
0f113f3e | 193 | |
ab4a81f6 DSH |
194 | ret = 1; |
195 | goto done; | |
0f113f3e MC |
196 | |
197 | decerr: | |
f6fb7f18 | 198 | DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR); |
0f113f3e | 199 | dsaerr: |
ab4a81f6 DSH |
200 | DSA_free(dsa); |
201 | done: | |
0f113f3e | 202 | BN_CTX_free(ctx); |
2ace7450 | 203 | ASN1_STRING_clear_free(privkey); |
ab4a81f6 | 204 | return ret; |
0f113f3e | 205 | } |
448be743 | 206 | |
6f81892e | 207 | static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) |
448be743 | 208 | { |
0f113f3e MC |
209 | ASN1_STRING *params = NULL; |
210 | ASN1_INTEGER *prkey = NULL; | |
211 | unsigned char *dp = NULL; | |
212 | int dplen; | |
213 | ||
12a765a5 | 214 | if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) { |
0f113f3e MC |
215 | DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS); |
216 | goto err; | |
217 | } | |
218 | ||
219 | params = ASN1_STRING_new(); | |
220 | ||
90945fa3 | 221 | if (params == NULL) { |
0f113f3e MC |
222 | DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); |
223 | goto err; | |
224 | } | |
225 | ||
226 | params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data); | |
227 | if (params->length <= 0) { | |
228 | DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); | |
229 | goto err; | |
230 | } | |
231 | params->type = V_ASN1_SEQUENCE; | |
232 | ||
233 | /* Get private key into integer */ | |
234 | prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL); | |
235 | ||
12a765a5 | 236 | if (prkey == NULL) { |
0f113f3e MC |
237 | DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR); |
238 | goto err; | |
239 | } | |
240 | ||
241 | dplen = i2d_ASN1_INTEGER(prkey, &dp); | |
242 | ||
a8ae0891 | 243 | ASN1_STRING_clear_free(prkey); |
fa4629b6 | 244 | prkey = NULL; |
0f113f3e MC |
245 | |
246 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0, | |
247 | V_ASN1_SEQUENCE, params, dp, dplen)) | |
248 | goto err; | |
249 | ||
250 | return 1; | |
251 | ||
252 | err: | |
b548a1f1 | 253 | OPENSSL_free(dp); |
0dfb9398 | 254 | ASN1_STRING_free(params); |
2ace7450 | 255 | ASN1_STRING_clear_free(prkey); |
0f113f3e | 256 | return 0; |
448be743 DSH |
257 | } |
258 | ||
6f81892e | 259 | static int int_dsa_size(const EVP_PKEY *pkey) |
0f113f3e | 260 | { |
26a7d938 | 261 | return DSA_size(pkey->pkey.dsa); |
0f113f3e | 262 | } |
6f81892e DSH |
263 | |
264 | static int dsa_bits(const EVP_PKEY *pkey) | |
0f113f3e | 265 | { |
5d8d9a8e | 266 | return DSA_bits(pkey->pkey.dsa); |
0f113f3e | 267 | } |
6f81892e | 268 | |
2514fa79 | 269 | static int dsa_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
270 | { |
271 | return DSA_security_bits(pkey->pkey.dsa); | |
272 | } | |
2514fa79 | 273 | |
6f81892e | 274 | static int dsa_missing_parameters(const EVP_PKEY *pkey) |
0f113f3e MC |
275 | { |
276 | DSA *dsa; | |
277 | dsa = pkey->pkey.dsa; | |
f72f00d4 | 278 | if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) |
0f113f3e MC |
279 | return 1; |
280 | return 0; | |
281 | } | |
6f81892e DSH |
282 | |
283 | static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) | |
0f113f3e MC |
284 | { |
285 | BIGNUM *a; | |
286 | ||
2986ecdc DSH |
287 | if (to->pkey.dsa == NULL) { |
288 | to->pkey.dsa = DSA_new(); | |
289 | if (to->pkey.dsa == NULL) | |
290 | return 0; | |
291 | } | |
292 | ||
0f113f3e MC |
293 | if ((a = BN_dup(from->pkey.dsa->p)) == NULL) |
294 | return 0; | |
23a1d5e9 | 295 | BN_free(to->pkey.dsa->p); |
0f113f3e MC |
296 | to->pkey.dsa->p = a; |
297 | ||
298 | if ((a = BN_dup(from->pkey.dsa->q)) == NULL) | |
299 | return 0; | |
23a1d5e9 | 300 | BN_free(to->pkey.dsa->q); |
0f113f3e MC |
301 | to->pkey.dsa->q = a; |
302 | ||
303 | if ((a = BN_dup(from->pkey.dsa->g)) == NULL) | |
304 | return 0; | |
23a1d5e9 | 305 | BN_free(to->pkey.dsa->g); |
0f113f3e | 306 | to->pkey.dsa->g = a; |
4889dadc | 307 | to->pkey.dsa->dirty_cnt++; |
0f113f3e MC |
308 | return 1; |
309 | } | |
6f81892e DSH |
310 | |
311 | static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) | |
0f113f3e MC |
312 | { |
313 | if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) || | |
314 | BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) || | |
315 | BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g)) | |
316 | return 0; | |
317 | else | |
318 | return 1; | |
319 | } | |
6f81892e | 320 | |
0cb8499b | 321 | static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
322 | { |
323 | if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0) | |
324 | return 0; | |
325 | else | |
326 | return 1; | |
327 | } | |
0cb8499b | 328 | |
6f81892e | 329 | static void int_dsa_free(EVP_PKEY *pkey) |
0f113f3e MC |
330 | { |
331 | DSA_free(pkey->pkey.dsa); | |
332 | } | |
6f81892e | 333 | |
777c47ac | 334 | static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) |
0f113f3e | 335 | { |
0f113f3e | 336 | int ret = 0; |
0f113f3e | 337 | const char *ktype = NULL; |
0f113f3e | 338 | const BIGNUM *priv_key, *pub_key; |
aecf529b | 339 | int mod_len = 0; |
340 | ||
341 | if (x->p != NULL) | |
342 | mod_len = BN_num_bits(x->p); | |
0f113f3e MC |
343 | |
344 | if (ptype == 2) | |
345 | priv_key = x->priv_key; | |
346 | else | |
347 | priv_key = NULL; | |
348 | ||
349 | if (ptype > 0) | |
350 | pub_key = x->pub_key; | |
351 | else | |
352 | pub_key = NULL; | |
353 | ||
354 | if (ptype == 2) | |
355 | ktype = "Private-Key"; | |
356 | else if (ptype == 1) | |
357 | ktype = "Public-Key"; | |
358 | else | |
359 | ktype = "DSA-Parameters"; | |
360 | ||
0f113f3e MC |
361 | if (priv_key) { |
362 | if (!BIO_indent(bp, off, 128)) | |
363 | goto err; | |
364 | if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) | |
365 | <= 0) | |
366 | goto err; | |
aecf529b | 367 | } else { |
368 | if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0) | |
369 | goto err; | |
0f113f3e MC |
370 | } |
371 | ||
a773b52a | 372 | if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off)) |
0f113f3e | 373 | goto err; |
a773b52a | 374 | if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off)) |
0f113f3e | 375 | goto err; |
a773b52a | 376 | if (!ASN1_bn_print(bp, "P: ", x->p, NULL, off)) |
0f113f3e | 377 | goto err; |
a773b52a | 378 | if (!ASN1_bn_print(bp, "Q: ", x->q, NULL, off)) |
0f113f3e | 379 | goto err; |
a773b52a | 380 | if (!ASN1_bn_print(bp, "G: ", x->g, NULL, off)) |
0f113f3e MC |
381 | goto err; |
382 | ret = 1; | |
383 | err: | |
26a7d938 | 384 | return ret; |
0f113f3e | 385 | } |
35208f36 | 386 | |
3e4585c8 | 387 | static int dsa_param_decode(EVP_PKEY *pkey, |
0f113f3e MC |
388 | const unsigned char **pder, int derlen) |
389 | { | |
390 | DSA *dsa; | |
75ebbd9a RS |
391 | |
392 | if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) { | |
0f113f3e MC |
393 | DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB); |
394 | return 0; | |
395 | } | |
4889dadc | 396 | dsa->dirty_cnt++; |
0f113f3e MC |
397 | EVP_PKEY_assign_DSA(pkey, dsa); |
398 | return 1; | |
399 | } | |
3e4585c8 DSH |
400 | |
401 | static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
402 | { |
403 | return i2d_DSAparams(pkey->pkey.dsa, pder); | |
404 | } | |
35208f36 DSH |
405 | |
406 | static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
407 | ASN1_PCTX *ctx) |
408 | { | |
409 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 0); | |
410 | } | |
35208f36 DSH |
411 | |
412 | static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
413 | ASN1_PCTX *ctx) |
414 | { | |
415 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 1); | |
416 | } | |
35208f36 DSH |
417 | |
418 | static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
419 | ASN1_PCTX *ctx) |
420 | { | |
421 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 2); | |
422 | } | |
35208f36 | 423 | |
e4263314 | 424 | static int old_dsa_priv_decode(EVP_PKEY *pkey, |
0f113f3e MC |
425 | const unsigned char **pder, int derlen) |
426 | { | |
427 | DSA *dsa; | |
75ebbd9a RS |
428 | |
429 | if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) { | |
0f113f3e MC |
430 | DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB); |
431 | return 0; | |
432 | } | |
4889dadc | 433 | dsa->dirty_cnt++; |
0f113f3e MC |
434 | EVP_PKEY_assign_DSA(pkey, dsa); |
435 | return 1; | |
436 | } | |
e4263314 DSH |
437 | |
438 | static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
439 | { |
440 | return i2d_DSAPrivateKey(pkey->pkey.dsa, pder); | |
441 | } | |
e4263314 | 442 | |
fa1ba589 | 443 | static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, |
0f113f3e MC |
444 | const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) |
445 | { | |
446 | DSA_SIG *dsa_sig; | |
447 | const unsigned char *p; | |
a773b52a | 448 | |
0f113f3e MC |
449 | if (!sig) { |
450 | if (BIO_puts(bp, "\n") <= 0) | |
451 | return 0; | |
452 | else | |
453 | return 1; | |
454 | } | |
455 | p = sig->data; | |
456 | dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length); | |
457 | if (dsa_sig) { | |
458 | int rv = 0; | |
9267c11b | 459 | const BIGNUM *r, *s; |
706a13f1 | 460 | |
9267c11b | 461 | DSA_SIG_get0(dsa_sig, &r, &s); |
0f113f3e MC |
462 | |
463 | if (BIO_write(bp, "\n", 1) != 1) | |
464 | goto err; | |
465 | ||
706a13f1 | 466 | if (!ASN1_bn_print(bp, "r: ", r, NULL, indent)) |
0f113f3e | 467 | goto err; |
706a13f1 | 468 | if (!ASN1_bn_print(bp, "s: ", s, NULL, indent)) |
0f113f3e MC |
469 | goto err; |
470 | rv = 1; | |
471 | err: | |
0f113f3e MC |
472 | DSA_SIG_free(dsa_sig); |
473 | return rv; | |
474 | } | |
a4c467c9 DO |
475 | if (BIO_puts(bp, "\n") <= 0) |
476 | return 0; | |
0f113f3e MC |
477 | return X509_signature_dump(bp, sig, indent); |
478 | } | |
fa1ba589 | 479 | |
492a9e24 | 480 | static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
0f113f3e MC |
481 | { |
482 | switch (op) { | |
483 | case ASN1_PKEY_CTRL_PKCS7_SIGN: | |
484 | if (arg1 == 0) { | |
485 | int snid, hnid; | |
486 | X509_ALGOR *alg1, *alg2; | |
487 | PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2); | |
488 | if (alg1 == NULL || alg1->algorithm == NULL) | |
489 | return -1; | |
490 | hnid = OBJ_obj2nid(alg1->algorithm); | |
491 | if (hnid == NID_undef) | |
492 | return -1; | |
493 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | |
494 | return -1; | |
495 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | |
496 | } | |
497 | return 1; | |
8931b30d | 498 | #ifndef OPENSSL_NO_CMS |
0f113f3e MC |
499 | case ASN1_PKEY_CTRL_CMS_SIGN: |
500 | if (arg1 == 0) { | |
501 | int snid, hnid; | |
502 | X509_ALGOR *alg1, *alg2; | |
503 | CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2); | |
504 | if (alg1 == NULL || alg1->algorithm == NULL) | |
505 | return -1; | |
506 | hnid = OBJ_obj2nid(alg1->algorithm); | |
507 | if (hnid == NID_undef) | |
508 | return -1; | |
509 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | |
510 | return -1; | |
511 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | |
512 | } | |
513 | return 1; | |
514 | ||
515 | case ASN1_PKEY_CTRL_CMS_RI_TYPE: | |
516 | *(int *)arg2 = CMS_RECIPINFO_NONE; | |
517 | return 1; | |
8931b30d | 518 | #endif |
492a9e24 | 519 | |
0f113f3e MC |
520 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
521 | *(int *)arg2 = NID_sha256; | |
cd4c83b5 | 522 | return 1; |
03919683 | 523 | |
0f113f3e MC |
524 | default: |
525 | return -2; | |
492a9e24 | 526 | |
0f113f3e | 527 | } |
492a9e24 | 528 | |
0f113f3e | 529 | } |
492a9e24 | 530 | |
4889dadc MC |
531 | static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey) |
532 | { | |
533 | return pkey->pkey.dsa->dirty_cnt; | |
534 | } | |
535 | ||
536 | static void *dsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt) | |
537 | { | |
538 | DSA *dsa = pk->pkey.dsa; | |
539 | OSSL_PARAM_BLD tmpl; | |
540 | const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa); | |
541 | const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa); | |
542 | const BIGNUM *priv_key = DSA_get0_priv_key(dsa); | |
543 | OSSL_PARAM *params; | |
544 | void *provkey = NULL; | |
545 | ||
546 | if (p == NULL || q == NULL || g == NULL) | |
547 | return NULL; | |
548 | ||
549 | ossl_param_bld_init(&tmpl); | |
550 | if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_P, p) | |
551 | || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_Q, q) | |
552 | || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_G, g)) | |
553 | return NULL; | |
554 | ||
555 | /* | |
556 | * This may be used to pass domain parameters only without any key data - | |
557 | * so "pub_key" is optional. We can never have a "priv_key" without a | |
558 | * corresponding "pub_key" though. | |
559 | */ | |
560 | if (pub_key != NULL) { | |
561 | if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY, | |
562 | pub_key)) | |
563 | return NULL; | |
564 | ||
565 | if (priv_key != NULL) { | |
566 | if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY, | |
567 | priv_key)) | |
568 | return NULL; | |
569 | } | |
570 | } | |
571 | ||
572 | params = ossl_param_bld_to_param(&tmpl); | |
573 | ||
574 | /* We export, the provider imports */ | |
575 | provkey = evp_keymgmt_importkey(keymgmt, params); | |
576 | ||
577 | ossl_param_bld_free(params); | |
578 | return provkey; | |
579 | } | |
580 | ||
448be743 DSH |
581 | /* NB these are sorted in pkey_id order, lowest first */ |
582 | ||
578b5514 | 583 | const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = { |
0f113f3e MC |
584 | |
585 | { | |
586 | EVP_PKEY_DSA2, | |
587 | EVP_PKEY_DSA, | |
588 | ASN1_PKEY_ALIAS}, | |
589 | ||
590 | { | |
591 | EVP_PKEY_DSA1, | |
592 | EVP_PKEY_DSA, | |
593 | ASN1_PKEY_ALIAS}, | |
594 | ||
595 | { | |
596 | EVP_PKEY_DSA4, | |
597 | EVP_PKEY_DSA, | |
598 | ASN1_PKEY_ALIAS}, | |
599 | ||
600 | { | |
601 | EVP_PKEY_DSA3, | |
602 | EVP_PKEY_DSA, | |
603 | ASN1_PKEY_ALIAS}, | |
604 | ||
605 | { | |
606 | EVP_PKEY_DSA, | |
607 | EVP_PKEY_DSA, | |
608 | 0, | |
609 | ||
610 | "DSA", | |
611 | "OpenSSL DSA method", | |
612 | ||
613 | dsa_pub_decode, | |
614 | dsa_pub_encode, | |
615 | dsa_pub_cmp, | |
616 | dsa_pub_print, | |
617 | ||
618 | dsa_priv_decode, | |
619 | dsa_priv_encode, | |
620 | dsa_priv_print, | |
621 | ||
622 | int_dsa_size, | |
623 | dsa_bits, | |
624 | dsa_security_bits, | |
625 | ||
626 | dsa_param_decode, | |
627 | dsa_param_encode, | |
628 | dsa_missing_parameters, | |
629 | dsa_copy_parameters, | |
630 | dsa_cmp_parameters, | |
631 | dsa_param_print, | |
632 | dsa_sig_print, | |
633 | ||
634 | int_dsa_free, | |
635 | dsa_pkey_ctrl, | |
636 | old_dsa_priv_decode, | |
4889dadc MC |
637 | old_dsa_priv_encode, |
638 | ||
639 | NULL, NULL, NULL, | |
640 | NULL, NULL, NULL, | |
641 | NULL, NULL, NULL, NULL, | |
642 | ||
643 | dsa_pkey_dirty_cnt, | |
644 | dsa_pkey_export_to | |
645 | } | |
0f113f3e | 646 | }; |