mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/pk.h"
markrad 0:cdf462088d13 3 #include "mbedtls/pem.h"
markrad 0:cdf462088d13 4 #include "mbedtls/oid.h"
markrad 0:cdf462088d13 5 /* END_HEADER */
markrad 0:cdf462088d13 6
markrad 0:cdf462088d13 7 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 8 * depends_on:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
markrad 0:cdf462088d13 9 * END_DEPENDENCIES
markrad 0:cdf462088d13 10 */
markrad 0:cdf462088d13 11
markrad 0:cdf462088d13 12 /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
markrad 0:cdf462088d13 13 void pk_write_pubkey_check( char *key_file )
markrad 0:cdf462088d13 14 {
markrad 0:cdf462088d13 15 mbedtls_pk_context key;
markrad 0:cdf462088d13 16 unsigned char buf[5000];
markrad 0:cdf462088d13 17 unsigned char check_buf[5000];
markrad 0:cdf462088d13 18 int ret;
markrad 0:cdf462088d13 19 FILE *f;
markrad 0:cdf462088d13 20 size_t ilen;
markrad 0:cdf462088d13 21
markrad 0:cdf462088d13 22 memset( buf, 0, sizeof( buf ) );
markrad 0:cdf462088d13 23 memset( check_buf, 0, sizeof( check_buf ) );
markrad 0:cdf462088d13 24
markrad 0:cdf462088d13 25 mbedtls_pk_init( &key );
markrad 0:cdf462088d13 26 TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &key, key_file ) == 0 );
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 ret = mbedtls_pk_write_pubkey_pem( &key, buf, sizeof( buf ));
markrad 0:cdf462088d13 29 TEST_ASSERT( ret == 0 );
markrad 0:cdf462088d13 30
markrad 0:cdf462088d13 31 f = fopen( key_file, "r" );
markrad 0:cdf462088d13 32 TEST_ASSERT( f != NULL );
markrad 0:cdf462088d13 33 ilen = fread( check_buf, 1, sizeof( check_buf ), f );
markrad 0:cdf462088d13 34 fclose( f );
markrad 0:cdf462088d13 35
markrad 0:cdf462088d13 36 TEST_ASSERT( ilen == strlen( (char *) buf ) );
markrad 0:cdf462088d13 37 TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );
markrad 0:cdf462088d13 38
markrad 0:cdf462088d13 39 exit:
markrad 0:cdf462088d13 40 mbedtls_pk_free( &key );
markrad 0:cdf462088d13 41 }
markrad 0:cdf462088d13 42 /* END_CASE */
markrad 0:cdf462088d13 43
markrad 0:cdf462088d13 44 /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
markrad 0:cdf462088d13 45 void pk_write_key_check( char *key_file )
markrad 0:cdf462088d13 46 {
markrad 0:cdf462088d13 47 mbedtls_pk_context key;
markrad 0:cdf462088d13 48 unsigned char buf[5000];
markrad 0:cdf462088d13 49 unsigned char check_buf[5000];
markrad 0:cdf462088d13 50 int ret;
markrad 0:cdf462088d13 51 FILE *f;
markrad 0:cdf462088d13 52 size_t ilen;
markrad 0:cdf462088d13 53
markrad 0:cdf462088d13 54 memset( buf, 0, sizeof( buf ) );
markrad 0:cdf462088d13 55 memset( check_buf, 0, sizeof( check_buf ) );
markrad 0:cdf462088d13 56
markrad 0:cdf462088d13 57 mbedtls_pk_init( &key );
markrad 0:cdf462088d13 58 TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 );
markrad 0:cdf462088d13 59
markrad 0:cdf462088d13 60 ret = mbedtls_pk_write_key_pem( &key, buf, sizeof( buf ));
markrad 0:cdf462088d13 61 TEST_ASSERT( ret == 0 );
markrad 0:cdf462088d13 62
markrad 0:cdf462088d13 63 f = fopen( key_file, "r" );
markrad 0:cdf462088d13 64 TEST_ASSERT( f != NULL );
markrad 0:cdf462088d13 65 ilen = fread( check_buf, 1, sizeof( check_buf ), f );
markrad 0:cdf462088d13 66 fclose( f );
markrad 0:cdf462088d13 67
markrad 0:cdf462088d13 68 TEST_ASSERT( ilen == strlen( (char *) buf ) );
markrad 0:cdf462088d13 69 TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );
markrad 0:cdf462088d13 70
markrad 0:cdf462088d13 71 exit:
markrad 0:cdf462088d13 72 mbedtls_pk_free( &key );
markrad 0:cdf462088d13 73 }
markrad 0:cdf462088d13 74 /* END_CASE */