diff options
| author | tb <> | 2023-08-10 09:43:51 +0000 |
|---|---|---|
| committer | tb <> | 2023-08-10 09:43:51 +0000 |
| commit | 7c964fe46edbb367e38d0f39655a7d180a1f49e2 (patch) | |
| tree | c6bcc699c6aecd20537e77897245dd9a46589ca8 /src/lib/libcrypto/dsa/dsa_ameth.c | |
| parent | b0e225428c3a2379158e29168f0774c946ef7768 (diff) | |
| download | openbsd-7c964fe46edbb367e38d0f39655a7d180a1f49e2.tar.gz openbsd-7c964fe46edbb367e38d0f39655a7d180a1f49e2.tar.bz2 openbsd-7c964fe46edbb367e38d0f39655a7d180a1f49e2.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/dsa_ameth.c')
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_ameth.c | 43 |
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: | |||
| 265 | static int | 265 | static int |
| 266 | dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) | 266 | dsa_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, ¶ms->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 | ||
| 304 | err: | 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 | ||
