1/* BEGIN_HEADER */ 2#include "mbedtls/pk.h" 3#include "mbedtls/pem.h" 4#include "mbedtls/oid.h" 5/* END_HEADER */ 6 7/* BEGIN_DEPENDENCIES 8 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO 9 * END_DEPENDENCIES 10 */ 11 12/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */ 13void pk_write_pubkey_check( char * key_file ) 14{ 15 mbedtls_pk_context key; 16 unsigned char buf[5000]; 17 unsigned char check_buf[5000]; 18 int ret; 19 FILE *f; 20 size_t ilen, pem_len, buf_index; 21 22 memset( buf, 0, sizeof( buf ) ); 23 memset( check_buf, 0, sizeof( check_buf ) ); 24 25 mbedtls_pk_init( &key ); 26 TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &key, key_file ) == 0 ); 27 28 ret = mbedtls_pk_write_pubkey_pem( &key, buf, sizeof( buf )); 29 TEST_ASSERT( ret == 0 ); 30 31 pem_len = strlen( (char *) buf ); 32 33 // check that the rest of the buffer remains clear 34 for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index ) 35 { 36 TEST_ASSERT( buf[buf_index] == 0 ); 37 } 38 39 f = fopen( key_file, "r" ); 40 TEST_ASSERT( f != NULL ); 41 ilen = fread( check_buf, 1, sizeof( check_buf ), f ); 42 fclose( f ); 43 44 TEST_ASSERT( ilen == pem_len ); 45 TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 ); 46 47exit: 48 mbedtls_pk_free( &key ); 49} 50/* END_CASE */ 51 52/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */ 53void pk_write_key_check( char * key_file ) 54{ 55 mbedtls_pk_context key; 56 unsigned char buf[5000]; 57 unsigned char check_buf[5000]; 58 int ret; 59 FILE *f; 60 size_t ilen, pem_len, buf_index; 61 62 memset( buf, 0, sizeof( buf ) ); 63 memset( check_buf, 0, sizeof( check_buf ) ); 64 65 mbedtls_pk_init( &key ); 66 TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL, 67 mbedtls_test_rnd_std_rand, NULL ) == 0 ); 68 69 ret = mbedtls_pk_write_key_pem( &key, buf, sizeof( buf )); 70 TEST_ASSERT( ret == 0 ); 71 72 pem_len = strlen( (char *) buf ); 73 74 // check that the rest of the buffer remains clear 75 for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index ) 76 { 77 TEST_ASSERT( buf[buf_index] == 0 ); 78 } 79 80 f = fopen( key_file, "r" ); 81 TEST_ASSERT( f != NULL ); 82 ilen = fread( check_buf, 1, sizeof( check_buf ), f ); 83 fclose( f ); 84 85 TEST_ASSERT( ilen == strlen( (char *) buf ) ); 86 TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 ); 87 88exit: 89 mbedtls_pk_free( &key ); 90} 91/* END_CASE */ 92