Skip to content

Commit e49a93d

Browse files
nyx191beldmit
authored andcommitted
Rewrite error handling in pub_decode_gost_ec()
1 parent 9869058 commit e49a93d

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

gost_ameth.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -906,58 +906,59 @@ static int pub_decode_gost_ec(EVP_PKEY *pk, const X509_PUBKEY *pub)
906906
{
907907
X509_ALGOR *palg = NULL;
908908
const unsigned char *pubkey_buf = NULL;
909-
unsigned char *databuf;
909+
unsigned char *databuf = NULL;
910910
ASN1_OBJECT *palgobj = NULL;
911911
int pub_len;
912-
EC_POINT *pub_key;
913-
BIGNUM *X, *Y;
912+
EC_POINT *pub_key = NULL;
913+
BIGNUM *X = NULL, *Y = NULL;
914914
ASN1_OCTET_STRING *octet = NULL;
915915
size_t len;
916916
const EC_GROUP *group;
917+
int retval = 0;
917918

918919
if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
919-
return 0;
920+
goto ret;
920921
EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
921922
if (!decode_gost_algor_params(pk, palg))
922-
return 0;
923+
goto ret;
923924
group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
924925
octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
925926
if (!octet) {
926927
GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
927-
return 0;
928+
goto ret;
928929
}
929930
databuf = OPENSSL_malloc(octet->length);
930-
if (databuf == NULL) {
931+
if (!databuf) {
931932
GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
932-
ASN1_OCTET_STRING_free(octet);
933-
return 0;
933+
goto ret;
934934
}
935935

936936
BUF_reverse(databuf, octet->data, octet->length);
937937
len = octet->length / 2;
938-
ASN1_OCTET_STRING_free(octet);
939938

940939
Y = BN_bin2bn(databuf, len, NULL);
941940
X = BN_bin2bn(databuf + len, len, NULL);
942-
OPENSSL_free(databuf);
941+
if (!X || !Y) {
942+
GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_BN_LIB);
943+
goto ret;
944+
}
943945
pub_key = EC_POINT_new(group);
944946
if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
945947
GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
946-
EC_POINT_free(pub_key);
947-
BN_free(X);
948-
BN_free(Y);
949-
return 0;
948+
goto ret;
950949
}
951-
BN_free(X);
952-
BN_free(Y);
953-
if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
950+
951+
retval = EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key);
952+
if (!retval)
954953
GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
955-
EC_POINT_free(pub_key);
956-
return 0;
957-
}
958-
EC_POINT_free(pub_key);
959-
return 1;
960954

955+
ret:
956+
EC_POINT_free(pub_key);
957+
BN_free(X);
958+
BN_free(Y);
959+
OPENSSL_free(databuf);
960+
ASN1_OCTET_STRING_free(octet);
961+
return retval;
961962
}
962963

963964
static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)

0 commit comments

Comments
 (0)