diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d75752ceb..bc40eb173 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,6 +40,7 @@ add_test_suite(aes aes.cbc) add_test_suite(aes aes.cfb) add_test_suite(aes aes.rest) add_test_suite(arc4) +add_test_suite(asn1write) add_test_suite(base64) add_test_suite(blowfish) add_test_suite(camellia) diff --git a/tests/Makefile b/tests/Makefile index 61e38c413..dd9f205b4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -32,7 +32,7 @@ endif APPS = test_suite_aes.ecb test_suite_aes.cbc \ test_suite_aes.cfb test_suite_aes.rest \ - test_suite_arc4 \ + test_suite_arc4 test_suite_asn1write \ test_suite_base64 test_suite_blowfish \ test_suite_camellia test_suite_ccm \ test_suite_cipher.aes \ @@ -192,6 +192,10 @@ test_suite_arc4: test_suite_arc4.c $(DEP) echo " CC $@.c" $(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@ +test_suite_asn1write: test_suite_asn1write.c $(DEP) + echo " CC $@.c" + $(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@ + test_suite_base64: test_suite_base64.c $(DEP) echo " CC $@.c" $(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@ diff --git a/tests/suites/test_suite_asn1write.data b/tests/suites/test_suite_asn1write.data new file mode 100644 index 000000000..c9b20d733 --- /dev/null +++ b/tests/suites/test_suite_asn1write.data @@ -0,0 +1,32 @@ +ASN.1 Write Octet String #0 (Empty string) +asn1_write_octet_string:"":"0400":2:2 + +ASN.1 Write Octet String #1 (Large buffer) +asn1_write_octet_string:"AABBCC":"0403AABBCC":10:5 + +ASN.1 Write Octet String #2 (Buffer just fits) +asn1_write_octet_string:"AABBCC":"0403AABBCC":5:5 + +ASN.1 Write Octet String #3 (Buffer too small for tag) +asn1_write_octet_string:"AABBCC":"0403AABBCC":4:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write Octet String #4 (Buffer too small for len) +asn1_write_octet_string:"AABBCC":"0403AABBCC":3:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write Octet String #5 (Buffer too small for string) +asn1_write_octet_string:"AABBCC":"0403AABBCC":2:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write Octet String #6 (l = 128, large buffer) +asn1_write_octet_string:"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":"048180000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":140:131 + +ASN.1 Write Octet String #7 (l = 128, buffer just fits) +asn1_write_octet_string:"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":"048180000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":131:131 + +ASN.1 Write Octet String #8 (l = 128, buffer too small for tag) +asn1_write_octet_string:"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":"":130:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write Octet String #9 (l = 128, buffer too small for len) +asn1_write_octet_string:"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":"":129:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write Octet String #9 (l = 128, buffer too small for string) +asn1_write_octet_string:"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":"":127:POLARSSL_ERR_ASN1_BUF_TOO_SMALL diff --git a/tests/suites/test_suite_asn1write.function b/tests/suites/test_suite_asn1write.function new file mode 100644 index 000000000..53cb739a4 --- /dev/null +++ b/tests/suites/test_suite_asn1write.function @@ -0,0 +1,48 @@ +/* BEGIN_HEADER */ +#include + +#define GUARD_LEN 4 +#define GUARD_VAL 0x2a +/* END_HEADER */ + +/* BEGIN_DEPENDENCIES + * depends_on:POLARSSL_ASN1_WRITE_C + * END_DEPENDENCIES + */ + +/* BEGIN_CASE */ +void asn1_write_octet_string( char *hex_str, char *hex_asn1, + int buf_len, int result ) +{ + int ret; + unsigned char buf[150]; + unsigned char str[150] = { 0 }; + unsigned char asn1[150] = { 0 }; + size_t str_len, asn1_len, i; + unsigned char *p; + + memset( buf, GUARD_VAL, sizeof( buf ) ); + + str_len = unhexify( str, hex_str ); + asn1_len = unhexify( asn1, hex_asn1 ); + + p = buf + GUARD_LEN + buf_len; + + ret = asn1_write_octet_string( &p, buf + GUARD_LEN, str, str_len ); + + /* Check for buffer overwrite on both sides */ + for( i = 0; i < GUARD_LEN; i++ ) + { + TEST_ASSERT( buf[i] == GUARD_VAL ); + TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL ); + } + + if( result >= 0 ) + { + TEST_ASSERT( (size_t) ret == asn1_len ); + TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len ); + + TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 ); + } +} +/* END_CASE */