File: test_suite_pkwrite.function

package info (click to toggle)
edk2 2025.02-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 271,704 kB
  • sloc: ansic: 2,109,987; asm: 263,832; perl: 227,730; python: 149,919; sh: 34,967; cpp: 21,813; makefile: 3,282; xml: 806; pascal: 721; lisp: 35; ruby: 16; sed: 6; tcl: 4
file content (91 lines) | stat: -rw-r--r-- 2,433 bytes parent folder | download
1
2
3
4
5
6
7
8
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* BEGIN_HEADER */
#include "mbedtls/pk.h"
#include "mbedtls/pem.h"
#include "mbedtls/oid.h"
/* END_HEADER */

/* BEGIN_DEPENDENCIES
 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
 * END_DEPENDENCIES
 */

/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
void pk_write_pubkey_check( char * key_file )
{
    mbedtls_pk_context key;
    unsigned char buf[5000];
    unsigned char check_buf[5000];
    int ret;
    FILE *f;
    size_t ilen, pem_len, buf_index;

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

    mbedtls_pk_init( &key );
    TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &key, key_file ) == 0 );

    ret = mbedtls_pk_write_pubkey_pem( &key, buf, sizeof( buf ));
    TEST_ASSERT( ret == 0 );

    pem_len = strlen( (char *) buf );

    // check that the rest of the buffer remains clear
    for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index )
    {
        TEST_ASSERT( buf[buf_index] == 0 );
    }

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

    TEST_ASSERT( ilen == pem_len );
    TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );

exit:
    mbedtls_pk_free( &key );
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
void pk_write_key_check( char * key_file )
{
    mbedtls_pk_context key;
    unsigned char buf[5000];
    unsigned char check_buf[5000];
    int ret;
    FILE *f;
    size_t ilen, pem_len, buf_index;

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

    mbedtls_pk_init( &key );
    TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL,
                        mbedtls_test_rnd_std_rand, NULL ) == 0 );

    ret = mbedtls_pk_write_key_pem( &key, buf, sizeof( buf ));
    TEST_ASSERT( ret == 0 );

    pem_len = strlen( (char *) buf );

    // check that the rest of the buffer remains clear
    for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index )
    {
        TEST_ASSERT( buf[buf_index] == 0 );
    }

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

    TEST_ASSERT( ilen == strlen( (char *) buf ) );
    TEST_ASSERT( memcmp( (char *) buf, (char *) check_buf, ilen ) == 0 );

exit:
    mbedtls_pk_free( &key );
}
/* END_CASE */