Added asn1_write_bitstring() and asn1_write_octet_string()

This commit is contained in:
Paul Bakker 2013-08-25 14:46:39 +02:00
parent ef0ba55a78
commit 598e450538
2 changed files with 52 additions and 3 deletions

View File

@ -48,6 +48,10 @@ int asn1_write_printable_string( unsigned char **p, unsigned char *start,
char *text );
int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
char *text );
int asn1_write_bitstring( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t bits );
int asn1_write_octet_string( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size );
#ifdef __cplusplus
}

View File

@ -216,9 +216,9 @@ int asn1_write_printable_string( unsigned char **p, unsigned char *start,
return( len );
}
int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
char *text )
char *text )
{
int ret;
size_t len = 0;
@ -238,6 +238,51 @@ int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
return( len );
}
int asn1_write_bitstring( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t bits )
{
int ret;
size_t len = 0, size;
size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
// Calculate byte length
//
if( *p - start < (int) size + 1 )
return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
len = size + 1;
(*p) -= size;
memcpy( *p, buf, size );
// Write unused bits
//
*--(*p) = size * 8 - bits;
ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
return( len );
}
int asn1_write_octet_string( unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size )
{
int ret;
size_t len = 0;
if( *p - start < (int) size + 1 )
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_tag( p, start, ASN1_OCTET_STRING ) );
return( len );
}
#endif