test_suite_pkwrite.function 1.58 KB
Newer Older
1 2 3 4 5 6 7
/* BEGIN_HEADER */
#include <polarssl/pk.h>
#include <polarssl/pem.h>
#include <polarssl/oid.h>
/* END_HEADER */

/* BEGIN_DEPENDENCIES
8
 * depends_on:POLARSSL_PK_C:POLARSSL_BIGNUM_C:POLARSSL_FS_IO
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 * END_DEPENDENCIES
 */

/* BEGIN_CASE */
void pk_write_pubkey_check( char *key_file )
{
    pk_context key;
    unsigned char buf[5000];
    unsigned char check_buf[5000];
    int ret;
    FILE *f;

    memset( buf, 0, sizeof( buf ) );
    memset( check_buf, 0, sizeof( check_buf ) );

    pk_init( &key );
    TEST_ASSERT( pk_parse_public_keyfile( &key, key_file ) == 0 );

    ret = pk_write_pubkey_pem( &key, buf, sizeof( buf ) - 1);
    TEST_ASSERT( ret >= 0 );

    f = fopen( key_file, "r" );
    TEST_ASSERT( f != NULL );
    fread( check_buf, 1, sizeof( check_buf ) - 1, f );
    fclose( f );

    TEST_ASSERT( strncmp( (char *) buf, (char *) check_buf, sizeof( buf ) ) == 0 );

    pk_free( &key );
}
/* END_CASE */

/* BEGIN_CASE */
void pk_write_key_check( char *key_file )
{
    pk_context key;
    unsigned char buf[5000];
    unsigned char check_buf[5000];
    int ret;
    FILE *f;

    memset( buf, 0, sizeof( buf ) );
    memset( check_buf, 0, sizeof( check_buf ) );

    pk_init( &key );
    TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );

    ret = pk_write_key_pem( &key, buf, sizeof( buf ) - 1);
    TEST_ASSERT( ret >= 0 );

    f = fopen( key_file, "r" );
    TEST_ASSERT( f != NULL );
    fread( check_buf, 1, sizeof( check_buf ) - 1, f );
    fclose( f );

    TEST_ASSERT( strncmp( (char *) buf, (char *) check_buf, sizeof( buf ) ) == 0 );

    pk_free( &key );
}
/* END_CASE */