Skip to content

Commit

Permalink
correcting mem allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
feventura committed Jan 22, 2024
1 parent 012ed3d commit 63bb39d
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions oqsprov/oqs_encode_key2any.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,13 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder)
} else {
if ((sk = sk_ASN1_TYPE_new_null()) == NULL)
return -1;
ASN1_TYPE *aType[oqsxkey->numkeys];
ASN1_OCTET_STRING *aString[oqsxkey->numkeys];
unsigned char *temp[oqsxkey->numkeys];
size_t templen[oqsxkey->numkeys];
ASN1_TYPE **aType
= OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE *));
ASN1_OCTET_STRING **aString
= OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_OCTET_STRING *));
unsigned char **temp
= OPENSSL_malloc(oqsxkey->numkeys * sizeof(unsigned char *));
size_t *templen = OPENSSL_malloc(oqsxkey->numkeys * sizeof(size_t));
int i;

for (i = 0; i < oqsxkey->numkeys; i++) {
Expand Down Expand Up @@ -577,6 +580,10 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder)

sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_secure_clear_free(buf, buflen);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
return -1;
}
OPENSSL_secure_clear_free(buf, buflen);
Expand All @@ -592,6 +599,10 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder)
}

sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);

return keybloblen;
}
Expand Down Expand Up @@ -695,10 +706,13 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder)
keybloblen = 0; // signal error
}
} else {
ASN1_TYPE *aType[oqsxkey->numkeys];
ASN1_OCTET_STRING *aString[oqsxkey->numkeys];
unsigned char *temp[oqsxkey->numkeys];
size_t templen[oqsxkey->numkeys];
ASN1_TYPE **aType
= OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE *));
ASN1_OCTET_STRING **aString
= OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_OCTET_STRING *));
unsigned char **temp
= OPENSSL_malloc(oqsxkey->numkeys * sizeof(unsigned char *));
size_t *templen = OPENSSL_malloc(oqsxkey->numkeys * sizeof(size_t));
int i;

if ((sk = sk_ASN1_TYPE_new_null()) == NULL)
Expand All @@ -725,6 +739,10 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder)
else
ASN1_TYPE_free(aType[i]);

OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
OPENSSL_free(name);
return -1;
}
Expand Down Expand Up @@ -754,6 +772,10 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder)
else
ASN1_TYPE_free(aType[i]);

OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
OPENSSL_free(name);
return -1;
}
Expand Down Expand Up @@ -789,6 +811,10 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder)

sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(name);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
OPENSSL_secure_clear_free(buf, buflen);
return -1;
}
Expand All @@ -810,6 +836,10 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder)
}

sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
}
OPENSSL_secure_clear_free(buf, buflen);
return keybloblen;
Expand Down

0 comments on commit 63bb39d

Please sign in to comment.