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