summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dsa
diff options
context:
space:
mode:
authortb <>2023-08-10 09:43:51 +0000
committertb <>2023-08-10 09:43:51 +0000
commit16cca863e0409e2a511a758a5cbf5d41b6a3a852 (patch)
treec6bcc699c6aecd20537e77897245dd9a46589ca8 /src/lib/libcrypto/dsa
parent0c08015642e4fb2a5ce32565ab942ffc9579d487 (diff)
downloadopenbsd-16cca863e0409e2a511a758a5cbf5d41b6a3a852.tar.gz
openbsd-16cca863e0409e2a511a758a5cbf5d41b6a3a852.tar.bz2
openbsd-16cca863e0409e2a511a758a5cbf5d41b6a3a852.zip
Various fixes in {dh,dsa}_priv_encode()
Avoid creating an ASN1_STRING with negative length, set type, data and length via ASN1_STRING_type_new() and ASN1_STRING_set0() instead of doing this manually. Check return value for i2d_ASN1_INTEGER() and use an intermediate ASN1_OBJECT instead of nested function calls. Finally, clear sensitive data with freezero(). ok jsing
Diffstat (limited to 'src/lib/libcrypto/dsa')
-rw-r--r--src/lib/libcrypto/dsa/dsa_ameth.c43
1 files changed, 24 insertions, 19 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_ameth.c b/src/lib/libcrypto/dsa/dsa_ameth.c
index 5a0c3116aa..ad5aa09cd0 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.43 2023/07/07 06:59:18 tb Exp $ */ 1/* $OpenBSD: dsa_ameth.c,v 1.44 2023/08/10 09:43:51 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 */
@@ -265,46 +265,51 @@ done:
265static int 265static int
266dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) 266dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
267{ 267{
268 const DSA *dsa = pkey->pkey.dsa;
268 ASN1_STRING *params = NULL; 269 ASN1_STRING *params = NULL;
269 ASN1_INTEGER *prkey = NULL; 270 ASN1_INTEGER *prkey = NULL;
270 unsigned char *dp = NULL; 271 ASN1_OBJECT *aobj;
271 int dplen; 272 unsigned char *data = NULL, *dp = NULL;
273 int datalen = 0, dplen = 0;
272 274
273 params = ASN1_STRING_new(); 275 if ((datalen = i2d_DSAparams(dsa, &data)) <= 0) {
274 if (!params) {
275 DSAerror(ERR_R_MALLOC_FAILURE); 276 DSAerror(ERR_R_MALLOC_FAILURE);
277 datalen = 0;
276 goto err; 278 goto err;
277 } 279 }
278 280 if ((params = ASN1_STRING_type_new(V_ASN1_SEQUENCE)) == NULL) {
279 params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
280 if (params->length <= 0) {
281 DSAerror(ERR_R_MALLOC_FAILURE); 281 DSAerror(ERR_R_MALLOC_FAILURE);
282 goto err; 282 goto err;
283 } 283 }
284 params->type = V_ASN1_SEQUENCE; 284 ASN1_STRING_set0(params, data, datalen);
285 data = NULL;
286 datalen = 0;
285 287
286 /* Get private key into integer */ 288 if ((prkey = BN_to_ASN1_INTEGER(dsa->priv_key, NULL)) == NULL) {
287 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
288 if (!prkey) {
289 DSAerror(DSA_R_BN_ERROR); 289 DSAerror(DSA_R_BN_ERROR);
290 goto err; 290 goto err;
291 } 291 }
292 292 if ((dplen = i2d_ASN1_INTEGER(prkey, &dp)) <= 0) {
293 dplen = i2d_ASN1_INTEGER(prkey, &dp); 293 DSAerror(ERR_R_MALLOC_FAILURE);
294 294 dplen = 0;
295 goto err;
296 }
295 ASN1_INTEGER_free(prkey); 297 ASN1_INTEGER_free(prkey);
296 prkey = NULL; 298 prkey = NULL;
297 299
298 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0, V_ASN1_SEQUENCE, 300 if ((aobj = OBJ_nid2obj(NID_dsa)) == NULL)
299 params, dp, dplen)) 301 goto err;
302 if (!PKCS8_pkey_set0(p8, aobj, 0, V_ASN1_SEQUENCE, params, dp, dplen))
300 goto err; 303 goto err;
301 304
302 return 1; 305 return 1;
303 306
304err: 307 err:
305 free(dp);
306 ASN1_STRING_free(params); 308 ASN1_STRING_free(params);
307 ASN1_INTEGER_free(prkey); 309 ASN1_INTEGER_free(prkey);
310 freezero(data, datalen);
311 freezero(dp, dplen);
312
308 return 0; 313 return 0;
309} 314}
310 315