Add mbedtls_asn1_write_len() support for 3 and 4 byte lengths

As a consequence also adds coverage for reading 3 and 4 byte lengths
(which were not covered before)
This commit is contained in:
Paul Bakker 2016-07-14 11:39:56 +01:00 committed by Simon Butcher
parent 5e8b77cd8c
commit c7d6bd4b5f
3 changed files with 52 additions and 18 deletions

View File

@ -40,8 +40,6 @@ extern "C" {
* \param start start of the buffer (for bounds-checking) * \param start start of the buffer (for bounds-checking)
* \param len the length to write * \param len the length to write
* *
* \note lengths over 65535 are not supported at the moment
*
* \return the length written or a negative error code * \return the length written or a negative error code
*/ */
int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len ); int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len );

View File

@ -41,11 +41,6 @@
int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len ) int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
{ {
// We don't support lengths over 65535 for now
//
if( len > 0xFFFF )
return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
if( len < 0x80 ) if( len < 0x80 )
{ {
if( *p - start < 1 ) if( *p - start < 1 )
@ -65,14 +60,43 @@ int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len
return( 2 ); return( 2 );
} }
if( *p - start < 3 ) if( len <= 0xFFFF )
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); {
if( *p - start < 3 )
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
*--(*p) = len % 256; *--(*p) = ( len ) & 0xFF;
*--(*p) = ( len / 256 ) % 256; *--(*p) = ( len >> 8 ) & 0xFF;
*--(*p) = 0x82; *--(*p) = 0x82;
return( 3 );
}
return( 3 ); if( len <= 0xFFFFFF )
{
if( *p - start < 4 )
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
*--(*p) = ( len ) & 0xFF;
*--(*p) = ( len >> 8 ) & 0xFF;
*--(*p) = ( len >> 16 ) & 0xFF;
*--(*p) = 0x83;
return( 4 );
}
if( len <= 0xFFFFFFFF )
{
if( *p - start < 5 )
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
*--(*p) = ( len ) & 0xFF;
*--(*p) = ( len >> 8 ) & 0xFF;
*--(*p) = ( len >> 16 ) & 0xFF;
*--(*p) = ( len >> 24 ) & 0xFF;
*--(*p) = 0x84;
return( 5 );
}
return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
} }
int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag ) int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )

View File

@ -67,14 +67,26 @@ mbedtls_asn1_write_len:255:"81FF":2:2
ASN.1 Write / Read Length #5 (Len = 255, buffer too small) ASN.1 Write / Read Length #5 (Len = 255, buffer too small)
mbedtls_asn1_write_len:255:"81FF":1:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL mbedtls_asn1_write_len:255:"81FF":1:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #6 (Len = 256, long form) ASN.1 Write / Read Length #6 (Len = 258, byte order)
mbedtls_asn1_write_len:256:"820100":3:3 mbedtls_asn1_write_len:258:"820102":3:3
ASN.1 Write / Read Length #7 (Len = 65535, max supported length) ASN.1 Write / Read Length #7 (Len = 65535, long form)
mbedtls_asn1_write_len:65535:"82FFFF":3:3 mbedtls_asn1_write_len:65535:"82FFFF":3:3
ASN.1 Write / Read Length #8 (Len = 65535, buffer too small) ASN.1 Write / Read Length #8 (Len = 65535, buffer too small)
mbedtls_asn1_write_len:65535:"82FFFF":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL mbedtls_asn1_write_len:65535:"82FFFF":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #9 (Len = 65536, not supported) ASN.1 Write / Read Length #9 (Len = 66051, byte order)
mbedtls_asn1_write_len:65536:"":0:MBEDTLS_ERR_ASN1_INVALID_LENGTH mbedtls_asn1_write_len:66051:"83010203":4:4
ASN.1 Write / Read Length #10 (Len = 16777215, long form)
mbedtls_asn1_write_len:16777215:"83FFFFFF":4:4
ASN.1 Write / Read Length #11 (Len = 16777215, buffer too small)
mbedtls_asn1_write_len:16777215:"83FFFFFF":3:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #12 (Len = 16909060, byte order)
mbedtls_asn1_write_len:16909060:"8401020304":5:5
ASN.1 Write / Read Length #12 (Len = 16909060, buffer too small)
mbedtls_asn1_write_len:16909060:"8401020304":4:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL