Moved asn1write funtions to use asn1_write_raw_buffer()

This commit is contained in:
Paul Bakker 2013-08-26 17:56:37 +02:00
parent 7accbced87
commit 9852d00de6
2 changed files with 36 additions and 64 deletions

View File

@ -59,6 +59,20 @@ int asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
*/ */
int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag ); int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag );
/**
* \brief Write raw buffer data
* Note: function works backwards in data buffer
*
* \param p reference to current position pointer
* \param start start of the buffer (for bounds-checking)
* \param buf data buffer to write
* \param size length of the data buffer
*
* \return the length written or a negative error code
*/
int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size );
#if defined(POLARSSL_BIGNUM_C) #if defined(POLARSSL_BIGNUM_C)
/** /**
* \brief Write a big number (ASN1_INTEGER) in ASN.1 format * \brief Write a big number (ASN1_INTEGER) in ASN.1 format
@ -179,21 +193,6 @@ int asn1_write_bitstring( unsigned char **p, unsigned char *start,
*/ */
int asn1_write_octet_string( unsigned char **p, unsigned char *start, int asn1_write_octet_string( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size ); const unsigned char *buf, size_t size );
/**
* \brief Write raw buffer data
* Note: function works backwards in data buffer
*
* \param p reference to current position pointer
* \param start start of the buffer (for bounds-checking)
* \param buf data buffer to write
* \param size length of the data buffer
*
* \return the length written or a negative error code
*/
int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size );
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -72,6 +72,21 @@ int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
return( 1 ); return( 1 );
} }
int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size )
{
size_t len = 0;
if( *p - start < (int) size )
return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
len = size;
(*p) -= len;
memcpy( *p, buf, len );
return( len );
}
#if defined(POLARSSL_BIGNUM_C) #if defined(POLARSSL_BIGNUM_C)
int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ) int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
{ {
@ -125,15 +140,8 @@ int asn1_write_oid( unsigned char **p, unsigned char *start, const char *oid )
int ret; int ret;
size_t len = 0; size_t len = 0;
// Write OID ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
// (const unsigned char *) oid, strlen( oid ) ) );
len = strlen( oid );
if( *p - start < (int) len )
return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
(*p) -= len;
memcpy( *p, oid, len );
ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) ); ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) ); ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
@ -201,15 +209,8 @@ int asn1_write_printable_string( unsigned char **p, unsigned char *start,
int ret; int ret;
size_t len = 0; size_t len = 0;
// Write string ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
// (const unsigned char *) text, strlen( text ) ) );
len = strlen( text );
if( *p - start < (int) len )
return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
(*p) -= len;
memcpy( *p, text, len );
ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) ); ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
@ -223,15 +224,8 @@ int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
int ret; int ret;
size_t len = 0; size_t len = 0;
// Write string ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
// (const unsigned char *) text, strlen( text ) ) );
len = strlen( text );
if( *p - start < (int) len )
return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
(*p) -= len;
memcpy( *p, text, len );
ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) ); ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
@ -272,32 +266,11 @@ int asn1_write_octet_string( unsigned char **p, unsigned char *start,
int ret; int ret;
size_t len = 0; size_t len = 0;
if( *p - start < (int) size ) ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
len = size;
(*p) -= len;
memcpy( *p, buf, len );
ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) ); ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
return( len ); return( len );
} }
int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size )
{
size_t len = 0;
if( *p - start < (int) size )
return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
len = size;
(*p) -= len;
memcpy( *p, buf, len );
return( len );
}
#endif #endif