Skip to content

Commit 02d5996

Browse files
Improve mbedtls_asn1_write_int to support values >255
mbedtls_asn1_write_int had an undocumented restriction to values that fit in a single octet. Fix this. Negative integers are still not supported.
1 parent cd39c8a commit 02d5996

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

library/asn1write.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,20 @@ int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
236236
int ret;
237237
size_t len = 0;
238238

239-
if( *p - start < 1 )
240-
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
241-
242-
len += 1;
243-
*--(*p) = val;
244-
245-
if( val > 0 && **p & 0x80 )
239+
do
246240
{
247241
if( *p - start < 1 )
248242
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
243+
len += 1;
244+
*--(*p) = val & 0xff;
245+
val >>= 8;
246+
}
247+
while( val > 0 );
249248

249+
if( **p & 0x80 )
250+
{
251+
if( *p - start < 1 )
252+
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
250253
*--(*p) = 0x00;
251254
len += 1;
252255
}

0 commit comments

Comments
 (0)