mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-01-28 18:32:56 +00:00
pkwrite.c: save stack usage for pk_write_key_pem
mbedtls_pk_write_key_pem would allocate 5679 bytes in writing a DER encoded RSA private 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:
parent
45ad306fbf
commit
c84086e55c
@ -786,7 +786,11 @@ int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *bu
|
||||
int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned char output_buf[PRV_DER_MAX_BYTES];
|
||||
unsigned char *output_buf = NULL;
|
||||
output_buf = calloc(1, PRV_DER_MAX_BYTES);
|
||||
if (output_buf == NULL) {
|
||||
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
||||
}
|
||||
const char *begin, *end;
|
||||
size_t olen = 0;
|
||||
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
||||
@ -799,7 +803,8 @@ int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf,
|
||||
int is_rsa_opaque = 0;
|
||||
#endif
|
||||
|
||||
if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) < 0) {
|
||||
if ((ret = mbedtls_pk_write_key_der(key, output_buf, PRV_DER_MAX_BYTES)) < 0) {
|
||||
free(output_buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -843,14 +848,19 @@ int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf,
|
||||
}
|
||||
} else
|
||||
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
||||
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
||||
{
|
||||
free(output_buf);
|
||||
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_pem_write_buffer(begin, end,
|
||||
output_buf + sizeof(output_buf) - ret,
|
||||
output_buf + PRV_DER_MAX_BYTES - ret,
|
||||
ret, buf, size, &olen)) != 0) {
|
||||
free(output_buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
free(output_buf);
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_PEM_WRITE_C */
|
||||
|
Loading…
x
Reference in New Issue
Block a user