pkwrite.c: save stack usage for pk_write_pubkey_pem

mbedtls_pk_write_pubkey_pem would allocate 2086 bytes in writing a DER
encoded RSA public key. To save stack usage significantly, we use
heap memory instead.

Signed-off-by: Yanray Wang <yanray.wang@arm.com>
This commit is contained in:
Yanray Wang 2023-08-11 15:03:51 +08:00
parent 26b7c93d9d
commit 45ad306fbf

View file

@ -759,20 +759,27 @@ int mbedtls_pk_write_key_der(const mbedtls_pk_context *key, unsigned char *buf,
int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size) int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
{ {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char output_buf[PUB_DER_MAX_BYTES]; unsigned char *output_buf = NULL;
output_buf = calloc(1, PUB_DER_MAX_BYTES);
if (output_buf == NULL) {
return MBEDTLS_ERR_PK_ALLOC_FAILED;
}
size_t olen = 0; size_t olen = 0;
if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
sizeof(output_buf))) < 0) { PUB_DER_MAX_BYTES)) < 0) {
free(output_buf);
return ret; return ret;
} }
if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
output_buf + sizeof(output_buf) - ret, output_buf + PUB_DER_MAX_BYTES - ret,
ret, buf, size, &olen)) != 0) { ret, buf, size, &olen)) != 0) {
free(output_buf);
return ret; return ret;
} }
free(output_buf);
return 0; return 0;
} }