test_suite_pkparse.function 2.83 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_PARSE_C:POLARSSL_BIGNUM_C
9 10 11
 * END_DEPENDENCIES
 */

12
/* BEGIN_CASE depends_on:POLARSSL_RSA_C:POLARSSL_FS_IO */
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
void pk_parse_keyfile_rsa( char *key_file, char *password, int result )
{
    pk_context ctx;
    int res;
    char *pwd = password;

    pk_init( &ctx );

    if( strcmp( pwd, "NULL" ) == 0 )
        pwd = NULL;

    res = pk_parse_keyfile( &ctx, key_file, pwd );

    TEST_ASSERT( res == result );

    if( res == 0 )
    {
        rsa_context *rsa;
        TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_RSA ) );
        rsa = pk_rsa( ctx );
        TEST_ASSERT( rsa_check_privkey( rsa ) == 0 );
    }

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

40
/* BEGIN_CASE depends_on:POLARSSL_RSA_C:POLARSSL_FS_IO */
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void pk_parse_public_keyfile_rsa( char *key_file, int result )
{
    pk_context ctx;
    int res;

    pk_init( &ctx );

    res = pk_parse_public_keyfile( &ctx, key_file );

    TEST_ASSERT( res == result );

    if( res == 0 )
    {
        rsa_context *rsa;
        TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_RSA ) );
        rsa = pk_rsa( ctx );
        TEST_ASSERT( rsa_check_pubkey( rsa ) == 0 );
    }

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

64
/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
void pk_parse_public_keyfile_ec( char *key_file, int result )
{
    pk_context ctx;
    int res;

    pk_init( &ctx );

    res = pk_parse_public_keyfile( &ctx, key_file );

    TEST_ASSERT( res == result );

    if( res == 0 )
    {
        ecp_keypair *eckey;
        TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_ECKEY ) );
        eckey = pk_ec( ctx );
        TEST_ASSERT( ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
    }

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

88
/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
void pk_parse_keyfile_ec( char *key_file, char *password, int result )
{
    pk_context ctx;
    int res;

    pk_init( &ctx );

    res = pk_parse_keyfile( &ctx, key_file, password );

    TEST_ASSERT( res == result );

    if( res == 0 )
    {
        ecp_keypair *eckey;
        TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_ECKEY ) );
        eckey = pk_ec( ctx );
        TEST_ASSERT( ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
    }

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

/* BEGIN_CASE depends_on:POLARSSL_RSA_C */
void pk_parse_key_rsa( char *key_data, char *result_str, int result )
{
    pk_context pk;
    unsigned char buf[2000];
    unsigned char output[2000];
    int data_len;
    ((void) result_str);

    pk_init( &pk );

    memset( buf, 0, 2000 );
    memset( output, 0, 2000 );

    data_len = unhexify( buf, key_data );

    TEST_ASSERT( pk_parse_key( &pk, buf, data_len, NULL, 0 ) == ( result ) );
    if( ( result ) == 0 )
    {
        TEST_ASSERT( 1 );
    }

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