mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-14 08:03:09 +03:00
Fix memleak in rsa.c (#35)
* fix memleak in rsa.c * don't alloc block from the start;check block;don't use goto * fix whitespaces
This commit is contained in:
24
crypto/rsa.c
24
crypto/rsa.c
@@ -145,13 +145,19 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
|
|||||||
uint8_t *out_data, int out_len, int is_decryption)
|
uint8_t *out_data, int out_len, int is_decryption)
|
||||||
{
|
{
|
||||||
const int byte_size = ctx->num_octets;
|
const int byte_size = ctx->num_octets;
|
||||||
int i = 0, size;
|
int i = 0, size = -1;
|
||||||
bigint *decrypted_bi, *dat_bi;
|
bigint *decrypted_bi, *dat_bi;
|
||||||
uint8_t *block = (uint8_t *)malloc(byte_size);
|
uint8_t *block = NULL;
|
||||||
int pad_count = 0;
|
int pad_count = 0;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
if (out_len < byte_size) /* check output has enough size */
|
if (out_len < byte_size) /* check output has enough size */
|
||||||
return -1;
|
break;
|
||||||
|
|
||||||
|
block = (uint8_t *)malloc(byte_size);
|
||||||
|
if (!block)
|
||||||
|
break;
|
||||||
|
|
||||||
memset(out_data, 0, out_len); /* initialise */
|
memset(out_data, 0, out_len); /* initialise */
|
||||||
|
|
||||||
@@ -168,13 +174,13 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
|
|||||||
bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
|
bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
|
||||||
|
|
||||||
if (block[i++] != 0) /* leading 0? */
|
if (block[i++] != 0) /* leading 0? */
|
||||||
return -1;
|
break;
|
||||||
|
|
||||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||||
if (is_decryption == 0) /* PKCS1.5 signing pads with "0xff"s */
|
if (is_decryption == 0) /* PKCS1.5 signing pads with "0xff"s */
|
||||||
{
|
{
|
||||||
if (block[i++] != 0x01) /* BT correct? */
|
if (block[i++] != 0x01) /* BT correct? */
|
||||||
return -1;
|
break;
|
||||||
|
|
||||||
while (block[i++] == 0xff && i < byte_size)
|
while (block[i++] == 0xff && i < byte_size)
|
||||||
pad_count++;
|
pad_count++;
|
||||||
@@ -183,7 +189,7 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
if (block[i++] != 0x02) /* BT correct? */
|
if (block[i++] != 0x02) /* BT correct? */
|
||||||
return -1;
|
break;
|
||||||
|
|
||||||
while (block[i++] && i < byte_size)
|
while (block[i++] && i < byte_size)
|
||||||
pad_count++;
|
pad_count++;
|
||||||
@@ -191,13 +197,17 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
|
|||||||
|
|
||||||
/* check separator byte 0x00 - and padding must be 8 or more bytes */
|
/* check separator byte 0x00 - and padding must be 8 or more bytes */
|
||||||
if (i == byte_size || pad_count < 8)
|
if (i == byte_size || pad_count < 8)
|
||||||
return -1;
|
break;
|
||||||
|
|
||||||
size = byte_size - i;
|
size = byte_size - i;
|
||||||
|
|
||||||
/* get only the bit we want */
|
/* get only the bit we want */
|
||||||
memcpy(out_data, &block[i], size);
|
memcpy(out_data, &block[i], size);
|
||||||
|
} while(false);
|
||||||
|
|
||||||
|
if(block)
|
||||||
free(block);
|
free(block);
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user