From 598e4505382ca23456096fc6abe092d100799361 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Sun, 25 Aug 2013 14:46:39 +0200 Subject: [PATCH] Added asn1_write_bitstring() and asn1_write_octet_string() --- include/polarssl/asn1write.h | 4 +++ library/asn1write.c | 51 +++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/include/polarssl/asn1write.h b/include/polarssl/asn1write.h index 459b28256..6e4a25798 100644 --- a/include/polarssl/asn1write.h +++ b/include/polarssl/asn1write.h @@ -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 } diff --git a/library/asn1write.c b/library/asn1write.c index b1d68392f..fc85bf990 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -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