2013-09-15 12:54:56 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2013-09-15 12:54:56 +00:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2019-01-31 13:20:20 +00:00
|
|
|
* depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
|
2013-09-15 12:54:56 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
|
2017-05-30 13:23:15 +00:00
|
|
|
void pk_write_pubkey_check( char * key_file )
|
2013-09-15 12:54:56 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_context key;
|
2013-09-15 12:54:56 +00:00
|
|
|
unsigned char buf[5000];
|
|
|
|
unsigned char check_buf[5000];
|
|
|
|
int ret;
|
|
|
|
FILE *f;
|
2014-04-17 14:08:20 +00:00
|
|
|
size_t ilen;
|
2013-09-15 12:54:56 +00:00
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
memset( check_buf, 0, sizeof( check_buf ) );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_init( &key );
|
|
|
|
TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &key, key_file ) == 0 );
|
2013-09-15 12:54:56 +00:00
|
|
|
|
2015-08-18 17:49:40 +00:00
|
|
|
ret = mbedtls_pk_write_pubkey_pem( &key, buf, sizeof( buf ));
|
|
|
|
TEST_ASSERT( ret == 0 );
|
2013-09-15 12:54:56 +00:00
|
|
|
|
|
|
|
f = fopen( key_file, "r" );
|
|
|
|
TEST_ASSERT( f != NULL );
|
2015-08-18 17:49:40 +00:00
|
|
|
ilen = fread( check_buf, 1, sizeof( check_buf ), f );
|
2013-09-15 12:54:56 +00:00
|
|
|
fclose( f );
|
|
|
|
|
2014-04-17 14:08:20 +00:00
|
|
|
TEST_ASSERT( ilen == strlen( (char *) buf ) );
|
2015-08-18 17:49:40 +00:00
|
|
|
TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );
|
2013-09-15 12:54:56 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &key );
|
2013-09-15 12:54:56 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
|
2017-05-30 13:23:15 +00:00
|
|
|
void pk_write_key_check( char * key_file )
|
2013-09-15 12:54:56 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_context key;
|
2013-09-15 12:54:56 +00:00
|
|
|
unsigned char buf[5000];
|
|
|
|
unsigned char check_buf[5000];
|
|
|
|
int ret;
|
|
|
|
FILE *f;
|
2014-04-17 14:08:20 +00:00
|
|
|
size_t ilen;
|
2013-09-15 12:54:56 +00:00
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
memset( check_buf, 0, sizeof( check_buf ) );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_init( &key );
|
|
|
|
TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 );
|
2013-09-15 12:54:56 +00:00
|
|
|
|
2015-08-18 17:49:40 +00:00
|
|
|
ret = mbedtls_pk_write_key_pem( &key, buf, sizeof( buf ));
|
2015-08-19 08:24:34 +00:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2013-09-15 12:54:56 +00:00
|
|
|
|
|
|
|
f = fopen( key_file, "r" );
|
|
|
|
TEST_ASSERT( f != NULL );
|
2015-08-18 17:49:40 +00:00
|
|
|
ilen = fread( check_buf, 1, sizeof( check_buf ), f );
|
2013-09-15 12:54:56 +00:00
|
|
|
fclose( f );
|
|
|
|
|
2014-04-17 14:08:20 +00:00
|
|
|
TEST_ASSERT( ilen == strlen( (char *) buf ) );
|
2015-08-18 17:49:40 +00:00
|
|
|
TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );
|
2013-09-15 12:54:56 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &key );
|
2013-09-15 12:54:56 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|