Skip to content

Commit c4638cc

Browse files
committed
Change size of preallocated buffer for pk_sign() calls
1 parent 31d1432 commit c4638cc

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

library/x509write_crt.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
#include "mbedtls/pem.h"
4646
#endif /* MBEDTLS_PEM_WRITE_C */
4747

48+
/*
49+
* For the currently used signature algorithms the buffer to store any signature
50+
* must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
51+
*/
52+
#if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
53+
#define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
54+
#else
55+
#define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
56+
#endif
57+
4858
void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
4959
{
5060
memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
@@ -317,7 +327,7 @@ int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf,
317327
size_t sig_oid_len = 0;
318328
unsigned char *c, *c2;
319329
unsigned char hash[64];
320-
unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
330+
unsigned char sig[SIGNATURE_MAX_SIZE];
321331
unsigned char tmp_buf[2048];
322332
size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
323333
size_t len = 0;

library/x509write_csr.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
#include "mbedtls/pem.h"
5050
#endif
5151

52+
/*
53+
* For the currently used signature algorithms the buffer to store any signature
54+
* must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
55+
*/
56+
#if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
57+
#define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
58+
#else
59+
#define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
60+
#endif
61+
5262
void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
5363
{
5464
memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
@@ -138,7 +148,7 @@ int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, s
138148
size_t sig_oid_len = 0;
139149
unsigned char *c, *c2;
140150
unsigned char hash[64];
141-
unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
151+
unsigned char sig[SIGNATURE_MAX_SIZE];
142152
unsigned char tmp_buf[2048];
143153
size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
144154
size_t len = 0;

programs/pkey/pk_sign.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ void mbedtls_param_failed( const char *failure_condition,
7272
}
7373
#endif
7474

75+
/*
76+
* For the currently used signature algorithms the buffer to store any signature
77+
* must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
78+
*/
79+
#if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
80+
#define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
81+
#else
82+
#define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
83+
#endif
84+
7585
int main( int argc, char *argv[] )
7686
{
7787
FILE *f;
@@ -81,7 +91,7 @@ int main( int argc, char *argv[] )
8191
mbedtls_entropy_context entropy;
8292
mbedtls_ctr_drbg_context ctr_drbg;
8393
unsigned char hash[32];
84-
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
94+
unsigned char buf[SIGNATURE_MAX_SIZE];
8595
char filename[512];
8696
const char *pers = "mbedtls_pk_sign";
8797
size_t olen = 0;

0 commit comments

Comments
 (0)