diff options
Diffstat (limited to 'src/lib/libcrypto/dsa')
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_ameth.c | 173 |
1 files changed, 94 insertions, 79 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_ameth.c b/src/lib/libcrypto/dsa/dsa_ameth.c index 28aafebc04..badd2d25b5 100644 --- a/src/lib/libcrypto/dsa/dsa_ameth.c +++ b/src/lib/libcrypto/dsa/dsa_ameth.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_ameth.c,v 1.46 2023/08/10 16:57:15 tb Exp $ */ | 1 | /* $OpenBSD: dsa_ameth.c,v 1.47 2023/08/11 11:32:19 tb 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 | */ |
@@ -75,31 +75,32 @@ | |||
75 | static int | 75 | static int |
76 | dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | 76 | dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) |
77 | { | 77 | { |
78 | const unsigned char *p, *pm; | 78 | X509_ALGOR *algor; |
79 | int pklen, pmlen; | ||
80 | int ptype; | 79 | int ptype; |
81 | const void *pval; | 80 | const void *pval; |
82 | const ASN1_STRING *pstr; | 81 | const ASN1_STRING *params; |
83 | X509_ALGOR *palg; | 82 | const unsigned char *key_der, *params_der, *p; |
84 | ASN1_INTEGER *public_key = NULL; | 83 | int key_len, params_len; |
85 | 84 | ASN1_INTEGER *key = NULL; | |
86 | DSA *dsa = NULL; | 85 | DSA *dsa = NULL; |
86 | int ret = 0; | ||
87 | 87 | ||
88 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | 88 | if (!X509_PUBKEY_get0_param(NULL, &key_der, &key_len, &algor, pubkey)) |
89 | return 0; | 89 | goto err; |
90 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | 90 | X509_ALGOR_get0(NULL, &ptype, &pval, algor); |
91 | 91 | ||
92 | if (ptype == V_ASN1_SEQUENCE) { | 92 | if (ptype == V_ASN1_SEQUENCE) { |
93 | pstr = pval; | 93 | params = pval; |
94 | pm = pstr->data; | 94 | params_der = params->data; |
95 | pmlen = pstr->length; | 95 | params_len = params->length; |
96 | 96 | ||
97 | if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) { | 97 | p = params_der; |
98 | if ((dsa = d2i_DSAparams(NULL, &p, params_len)) == NULL) { | ||
98 | DSAerror(DSA_R_DECODE_ERROR); | 99 | DSAerror(DSA_R_DECODE_ERROR); |
99 | goto err; | 100 | goto err; |
100 | } | 101 | } |
101 | } else if (ptype == V_ASN1_NULL || ptype == V_ASN1_UNDEF) { | 102 | } else if (ptype == V_ASN1_NULL || ptype == V_ASN1_UNDEF) { |
102 | if (!(dsa = DSA_new())) { | 103 | if ((dsa = DSA_new()) == NULL) { |
103 | DSAerror(ERR_R_MALLOC_FAILURE); | 104 | DSAerror(ERR_R_MALLOC_FAILURE); |
104 | goto err; | 105 | goto err; |
105 | } | 106 | } |
@@ -108,31 +109,32 @@ dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | |||
108 | goto err; | 109 | goto err; |
109 | } | 110 | } |
110 | 111 | ||
111 | if (!(public_key = d2i_ASN1_INTEGER(NULL, &p, pklen))) { | 112 | p = key_der; |
113 | if ((key = d2i_ASN1_INTEGER(NULL, &p, key_len)) == NULL) { | ||
112 | DSAerror(DSA_R_DECODE_ERROR); | 114 | DSAerror(DSA_R_DECODE_ERROR); |
113 | goto err; | 115 | goto err; |
114 | } | 116 | } |
115 | 117 | if ((dsa->pub_key = ASN1_INTEGER_to_BN(key, NULL)) == NULL) { | |
116 | if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) { | ||
117 | DSAerror(DSA_R_BN_DECODE_ERROR); | 118 | DSAerror(DSA_R_BN_DECODE_ERROR); |
118 | goto err; | 119 | goto err; |
119 | } | 120 | } |
120 | |||
121 | /* We can only check for key consistency if we have parameters. */ | 121 | /* We can only check for key consistency if we have parameters. */ |
122 | if (ptype == V_ASN1_SEQUENCE) { | 122 | if (ptype == V_ASN1_SEQUENCE) { |
123 | if (!dsa_check_key(dsa)) | 123 | if (!dsa_check_key(dsa)) |
124 | goto err; | 124 | goto err; |
125 | } | 125 | } |
126 | 126 | ||
127 | ASN1_INTEGER_free(public_key); | 127 | if (!EVP_PKEY_assign_DSA(pkey, dsa)) |
128 | EVP_PKEY_assign_DSA(pkey, dsa); | 128 | goto err; |
129 | return 1; | 129 | dsa = NULL; |
130 | 130 | ||
131 | err: | 131 | ret = 1; |
132 | if (public_key) | 132 | |
133 | ASN1_INTEGER_free(public_key); | 133 | err: |
134 | ASN1_INTEGER_free(key); | ||
134 | DSA_free(dsa); | 135 | DSA_free(dsa); |
135 | return 0; | 136 | |
137 | return ret; | ||
136 | } | 138 | } |
137 | 139 | ||
138 | static int | 140 | static int |
@@ -192,81 +194,85 @@ dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) | |||
192 | return ret; | 194 | return ret; |
193 | } | 195 | } |
194 | 196 | ||
195 | /* In PKCS#8 DSA: you just get a private key integer and parameters in the | 197 | /* |
198 | * In PKCS#8 DSA: you just get a private key integer and parameters in the | ||
196 | * AlgorithmIdentifier the pubkey must be recalculated. | 199 | * AlgorithmIdentifier the pubkey must be recalculated. |
197 | */ | 200 | */ |
198 | static int | 201 | static int |
199 | dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) | 202 | dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) |
200 | { | 203 | { |
201 | const unsigned char *p, *pm; | 204 | const X509_ALGOR *algor; |
202 | int pklen, pmlen; | ||
203 | int ptype; | 205 | int ptype; |
204 | const void *pval; | 206 | const void *pval; |
205 | const ASN1_STRING *pstr; | 207 | const ASN1_STRING *params; |
206 | const X509_ALGOR *palg; | 208 | const unsigned char *key_der, *params_der, *p; |
207 | ASN1_INTEGER *privkey = NULL; | 209 | int key_len, params_len; |
210 | ASN1_INTEGER *key = NULL; | ||
208 | BN_CTX *ctx = NULL; | 211 | BN_CTX *ctx = NULL; |
209 | DSA *dsa = NULL; | 212 | DSA *dsa = NULL; |
210 | int ret = 0; | 213 | int ret = 0; |
211 | 214 | ||
212 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) | 215 | if (!PKCS8_pkey_get0(NULL, &key_der, &key_len, &algor, p8)) |
213 | return 0; | 216 | goto err; |
214 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | 217 | X509_ALGOR_get0(NULL, &ptype, &pval, algor); |
215 | if (ptype != V_ASN1_SEQUENCE) | 218 | |
216 | goto decerr; | 219 | if (ptype != V_ASN1_SEQUENCE) { |
217 | 220 | DSAerror(DSA_R_PARAMETER_ENCODING_ERROR); | |
218 | if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) | 221 | goto err; |
219 | goto decerr; | 222 | } |
220 | if (privkey->type == V_ASN1_NEG_INTEGER) | 223 | |
221 | goto decerr; | 224 | params = pval; |
222 | 225 | params_der = params->data; | |
223 | pstr = pval; | 226 | params_len = params->length; |
224 | pm = pstr->data; | 227 | |
225 | pmlen = pstr->length; | 228 | p = params_der; |
226 | if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) | 229 | if ((dsa = d2i_DSAparams(NULL, &p, params_len)) == NULL) { |
227 | goto decerr; | 230 | DSAerror(DSA_R_DECODE_ERROR); |
228 | /* We have parameters now set private key */ | 231 | goto err; |
229 | if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) { | 232 | } |
230 | DSAerror(DSA_R_BN_ERROR); | 233 | p = key_der; |
231 | goto dsaerr; | 234 | if ((key = d2i_ASN1_INTEGER(NULL, &p, key_len)) == NULL) { |
235 | DSAerror(DSA_R_DECODE_ERROR); | ||
236 | goto err; | ||
237 | } | ||
238 | if ((dsa->priv_key = ASN1_INTEGER_to_BN(key, NULL)) == NULL) { | ||
239 | DSAerror(DSA_R_BN_DECODE_ERROR); | ||
240 | goto err; | ||
232 | } | 241 | } |
233 | 242 | ||
234 | /* Check the key for basic consistency before doing expensive things. */ | 243 | /* Check the key for basic consistency before doing expensive things. */ |
235 | if (!dsa_check_key(dsa)) | 244 | if (!dsa_check_key(dsa)) |
236 | goto dsaerr; | 245 | goto err; |
237 | 246 | ||
238 | /* Calculate public key */ | 247 | /* Calculate public key */ |
239 | if (!(dsa->pub_key = BN_new())) { | 248 | if ((dsa->pub_key = BN_new()) == NULL) { |
240 | DSAerror(ERR_R_MALLOC_FAILURE); | 249 | DSAerror(ERR_R_MALLOC_FAILURE); |
241 | goto dsaerr; | 250 | goto err; |
242 | } | 251 | } |
243 | 252 | ||
244 | if ((ctx = BN_CTX_new()) == NULL) { | 253 | if ((ctx = BN_CTX_new()) == NULL) { |
245 | DSAerror(ERR_R_MALLOC_FAILURE); | 254 | DSAerror(ERR_R_MALLOC_FAILURE); |
246 | goto dsaerr; | 255 | goto err; |
247 | } | 256 | } |
248 | 257 | ||
249 | BN_CTX_start(ctx); | 258 | BN_CTX_start(ctx); |
250 | 259 | ||
251 | if (!BN_mod_exp_ct(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { | 260 | if (!BN_mod_exp_ct(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { |
252 | DSAerror(DSA_R_BN_ERROR); | 261 | DSAerror(DSA_R_BN_ERROR); |
253 | goto dsaerr; | 262 | goto err; |
254 | } | 263 | } |
255 | 264 | ||
256 | if (!EVP_PKEY_assign_DSA(pkey, dsa)) | 265 | if (!EVP_PKEY_assign_DSA(pkey, dsa)) |
257 | goto decerr; | 266 | goto err; |
267 | dsa = NULL; | ||
258 | 268 | ||
259 | ret = 1; | 269 | ret = 1; |
260 | goto done; | ||
261 | 270 | ||
262 | decerr: | 271 | err: |
263 | DSAerror(DSA_R_DECODE_ERROR); | ||
264 | dsaerr: | ||
265 | DSA_free(dsa); | 272 | DSA_free(dsa); |
266 | done: | ||
267 | BN_CTX_end(ctx); | 273 | BN_CTX_end(ctx); |
268 | BN_CTX_free(ctx); | 274 | BN_CTX_free(ctx); |
269 | ASN1_INTEGER_free(privkey); | 275 | ASN1_INTEGER_free(key); |
270 | 276 | ||
271 | return ret; | 277 | return ret; |
272 | } | 278 | } |
@@ -454,18 +460,25 @@ do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) | |||
454 | static int | 460 | static int |
455 | dsa_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) | 461 | dsa_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) |
456 | { | 462 | { |
457 | DSA *dsa; | 463 | DSA *dsa = NULL; |
464 | int ret = 0; | ||
458 | 465 | ||
459 | if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) { | 466 | if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) { |
460 | DSAerror(ERR_R_DSA_LIB); | 467 | DSAerror(ERR_R_DSA_LIB); |
461 | return 0; | 468 | goto err; |
462 | } | ||
463 | if (!dsa_check_key(dsa)) { | ||
464 | DSA_free(dsa); | ||
465 | return 0; | ||
466 | } | 469 | } |
467 | EVP_PKEY_assign_DSA(pkey, dsa); | 470 | if (!dsa_check_key(dsa)) |
468 | return 1; | 471 | goto err; |
472 | if (!EVP_PKEY_assign_DSA(pkey, dsa)) | ||
473 | goto err; | ||
474 | dsa = NULL; | ||
475 | |||
476 | ret = 1; | ||
477 | |||
478 | err: | ||
479 | DSA_free(dsa); | ||
480 | |||
481 | return ret; | ||
469 | } | 482 | } |
470 | 483 | ||
471 | static int | 484 | static int |
@@ -495,9 +508,10 @@ dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) | |||
495 | static int | 508 | static int |
496 | old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) | 509 | old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) |
497 | { | 510 | { |
498 | DSA *dsa; | 511 | DSA *dsa = NULL; |
499 | BN_CTX *ctx = NULL; | 512 | BN_CTX *ctx = NULL; |
500 | BIGNUM *result; | 513 | BIGNUM *result; |
514 | int ret = 0; | ||
501 | 515 | ||
502 | if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) { | 516 | if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) { |
503 | DSAerror(ERR_R_DSA_LIB); | 517 | DSAerror(ERR_R_DSA_LIB); |
@@ -551,17 +565,18 @@ old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) | |||
551 | goto err; | 565 | goto err; |
552 | } | 566 | } |
553 | 567 | ||
554 | BN_CTX_end(ctx); | 568 | if (!EVP_PKEY_assign_DSA(pkey, dsa)) |
555 | BN_CTX_free(ctx); | 569 | goto err; |
570 | dsa = NULL; | ||
556 | 571 | ||
557 | EVP_PKEY_assign_DSA(pkey, dsa); | 572 | ret = 1; |
558 | return 1; | ||
559 | 573 | ||
560 | err: | 574 | err: |
561 | BN_CTX_end(ctx); | 575 | BN_CTX_end(ctx); |
562 | BN_CTX_free(ctx); | 576 | BN_CTX_free(ctx); |
563 | DSA_free(dsa); | 577 | DSA_free(dsa); |
564 | return 0; | 578 | |
579 | return ret; | ||
565 | } | 580 | } |
566 | 581 | ||
567 | static int | 582 | static int |