1/* BEGIN_HEADER */ 2#include "mbedtls/asn1write.h" 3 4#define GUARD_LEN 4 5#define GUARD_VAL 0x2a 6/* END_HEADER */ 7 8/* BEGIN_DEPENDENCIES 9 * depends_on:MBEDTLS_ASN1_WRITE_C 10 * END_DEPENDENCIES 11 */ 12 13/* BEGIN_CASE */ 14void mbedtls_asn1_write_octet_string( char *hex_str, char *hex_asn1, 15 int buf_len, int result ) 16{ 17 int ret; 18 unsigned char buf[150]; 19 unsigned char str[150] = { 0 }; 20 unsigned char asn1[150] = { 0 }; 21 size_t str_len, asn1_len, i; 22 unsigned char *p; 23 24 memset( buf, GUARD_VAL, sizeof( buf ) ); 25 26 str_len = unhexify( str, hex_str ); 27 asn1_len = unhexify( asn1, hex_asn1 ); 28 29 p = buf + GUARD_LEN + buf_len; 30 31 ret = mbedtls_asn1_write_octet_string( &p, buf + GUARD_LEN, str, str_len ); 32 33 /* Check for buffer overwrite on both sides */ 34 for( i = 0; i < GUARD_LEN; i++ ) 35 { 36 TEST_ASSERT( buf[i] == GUARD_VAL ); 37 TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL ); 38 } 39 40 if( result >= 0 ) 41 { 42 TEST_ASSERT( (size_t) ret == asn1_len ); 43 TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len ); 44 45 TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 ); 46 } 47} 48/* END_CASE */ 49 50/* BEGIN_CASE */ 51void mbedtls_asn1_write_ia5_string( char *str, char *hex_asn1, 52 int buf_len, int result ) 53{ 54 int ret; 55 unsigned char buf[150]; 56 unsigned char asn1[150] = { 0 }; 57 size_t str_len, asn1_len, i; 58 unsigned char *p; 59 60 memset( buf, GUARD_VAL, sizeof( buf ) ); 61 62 str_len = strlen( str ); 63 asn1_len = unhexify( asn1, hex_asn1 ); 64 65 p = buf + GUARD_LEN + buf_len; 66 67 ret = mbedtls_asn1_write_ia5_string( &p, buf + GUARD_LEN, str, str_len ); 68 69 /* Check for buffer overwrite on both sides */ 70 for( i = 0; i < GUARD_LEN; i++ ) 71 { 72 TEST_ASSERT( buf[i] == GUARD_VAL ); 73 TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL ); 74 } 75 76 if( result >= 0 ) 77 { 78 TEST_ASSERT( (size_t) ret == asn1_len ); 79 TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len ); 80 81 TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 ); 82 } 83} 84/* END_CASE */ 85 86/* BEGIN_CASE */ 87void mbedtls_asn1_write_len( int len, char *check_str, int buf_len, 88 int result ) 89{ 90 int ret; 91 unsigned char buf[150]; 92 unsigned char asn1[150]; 93 unsigned char *p; 94 size_t asn1_len, i, read_len; 95 96 memset( buf, GUARD_VAL, sizeof( buf ) ); 97 memset( asn1, 0, sizeof( asn1 ) ); 98 asn1_len = unhexify( asn1, check_str ); 99 100 p = buf + GUARD_LEN + buf_len; 101 102 ret = mbedtls_asn1_write_len( &p, buf + GUARD_LEN, (size_t) len ); 103 104 TEST_ASSERT( ret == result ); 105 106 /* Check for buffer overwrite on both sides */ 107 for( i = 0; i < GUARD_LEN; i++ ) 108 { 109 TEST_ASSERT( buf[i] == GUARD_VAL ); 110 TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL ); 111 } 112 113 if( result >= 0 ) 114 { 115 TEST_ASSERT( (size_t) ret == asn1_len ); 116 TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len ); 117 118 TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 ); 119 120 /* Read back with mbedtls_asn1_get_len() to check */ 121 ret = mbedtls_asn1_get_len( &p, buf + GUARD_LEN + buf_len, &read_len ); 122 123 if( len == 0 ) 124 { 125 TEST_ASSERT( ret == 0 ); 126 } 127 else 128 { 129 /* Return will be MBEDTLS_ERR_ASN1_OUT_OF_DATA because the rest of 130 * the buffer is missing 131 */ 132 TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_OUT_OF_DATA ); 133 } 134 TEST_ASSERT( read_len == (size_t) len ); 135 TEST_ASSERT( p == buf + GUARD_LEN + buf_len ); 136 } 137} 138/* END_CASE */ 139