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_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;
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    f = fopen( key_file, "r" );
32    TEST_ASSERT( f != NULL );
33    ilen = fread( check_buf, 1, sizeof( check_buf ), f );
34    fclose( f );
35
36    TEST_ASSERT( ilen == strlen( (char *) buf ) );
37    TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );
38
39exit:
40    mbedtls_pk_free( &key );
41}
42/* END_CASE */
43
44/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
45void pk_write_key_check( char *key_file )
46{
47    mbedtls_pk_context key;
48    unsigned char buf[5000];
49    unsigned char check_buf[5000];
50    int ret;
51    FILE *f;
52    size_t ilen;
53
54    memset( buf, 0, sizeof( buf ) );
55    memset( check_buf, 0, sizeof( check_buf ) );
56
57    mbedtls_pk_init( &key );
58    TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 );
59
60    ret = mbedtls_pk_write_key_pem( &key, buf, sizeof( buf ));
61    TEST_ASSERT( ret == 0 );
62
63    f = fopen( key_file, "r" );
64    TEST_ASSERT( f != NULL );
65    ilen = fread( check_buf, 1, sizeof( check_buf ), f );
66    fclose( f );
67
68    TEST_ASSERT( ilen == strlen( (char *) buf ) );
69    TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );
70
71exit:
72    mbedtls_pk_free( &key );
73}
74/* END_CASE */
75