test_suite_x509parse.function 5.93 KB
Newer Older
1
/* BEGIN_HEADER */
2 3
#include <polarssl/x509_crt.h>
#include <polarssl/x509_crl.h>
4
#include <polarssl/pem.h>
5
#include <polarssl/oid.h>
6

7
int verify_none( void *data, x509_cert *crt, int certificate_depth, int *flags )
8
{
Paul Bakker's avatar
Paul Bakker committed
9 10 11
    ((void) data);
    ((void) crt);
    ((void) certificate_depth);
12 13 14
    *flags |= BADCERT_OTHER;
    
    return 0;
15 16
}

17
int verify_all( void *data, x509_cert *crt, int certificate_depth, int *flags )
18
{
Paul Bakker's avatar
Paul Bakker committed
19 20 21
    ((void) data);
    ((void) crt);
    ((void) certificate_depth);
22
    *flags = 0;
Paul Bakker's avatar
Paul Bakker committed
23

24 25 26
    return 0;
}

27
/* END_HEADER */
28

29
/* BEGIN_DEPENDENCIES
30
 * depends_on:POLARSSL_BIGNUM_C
31 32
 * END_DEPENDENCIES
 */
33

34
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
35
void x509_cert_info( char *crt_file, char *result_str )
36 37 38
{
    x509_cert   crt;
    char buf[2000];
39
    int res;
40 41 42 43

    memset( &crt, 0, sizeof( x509_cert ) );
    memset( buf, 0, 2000 );

44
    TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
45
    res = x509parse_cert_info( buf, 2000, "", &crt );
46

47
    x509_crt_free( &crt );
Paul Bakker's avatar
Paul Bakker committed
48

49 50 51
    TEST_ASSERT( res != -1 );
    TEST_ASSERT( res != -2 );

52
    TEST_ASSERT( strcmp( buf, result_str ) == 0 );
53
}
54
/* END_CASE */
55

56
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */
57
void x509_crl_info( char *crl_file, char *result_str )
58 59 60
{
    x509_crl   crl;
    char buf[2000];
61
    int res;
62 63 64 65

    memset( &crl, 0, sizeof( x509_crl ) );
    memset( buf, 0, 2000 );

66
    TEST_ASSERT( x509parse_crlfile( &crl, crl_file ) == 0 );
67
    res = x509parse_crl_info( buf, 2000, "", &crl );
68

Paul Bakker's avatar
Paul Bakker committed
69 70
    x509_crl_free( &crl );

71 72 73
    TEST_ASSERT( res != -1 );
    TEST_ASSERT( res != -2 );

74
    TEST_ASSERT( strcmp( buf, result_str ) == 0 );
75
}
76
/* END_CASE */
77

78
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
79 80 81
void x509_verify( char *crt_file, char *ca_file, char *crl_file,
                  char *cn_name_str, int result, int flags_result,
                  char *verify_callback )
82 83 84 85 86
{
    x509_cert   crt;
    x509_cert   ca;
    x509_crl    crl;
    int         flags = 0;
87
    int         res;
88 89
    int (*f_vrfy)(void *, x509_cert *, int, int *) = NULL;
    char *      cn_name = NULL;
90 91 92 93 94

    memset( &crt, 0, sizeof( x509_cert ) );
    memset( &ca, 0, sizeof( x509_cert ) );
    memset( &crl, 0, sizeof( x509_crl ) );

95 96
    if( strcmp( cn_name_str, "NULL" ) != 0 )
        cn_name = cn_name_str;
97

98
    if( strcmp( verify_callback, "NULL" ) == 0 )
99
        f_vrfy = NULL;
100
    else if( strcmp( verify_callback, "verify_none" ) == 0 )
101
        f_vrfy = verify_none;
102
    else if( strcmp( verify_callback, "verify_all" ) == 0 )
103 104 105 106
        f_vrfy = verify_all;
    else
        TEST_ASSERT( "No known verify callback selected" == 0 );

107 108 109
    TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
    TEST_ASSERT( x509parse_crtfile( &ca, ca_file ) == 0 );
    TEST_ASSERT( x509parse_crlfile( &crl, crl_file ) == 0 );
110

111
    res = x509parse_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
112

113 114
    x509_crt_free( &crt );
    x509_crt_free( &ca );
Paul Bakker's avatar
Paul Bakker committed
115 116
    x509_crl_free( &crl );

117 118
    TEST_ASSERT( res == ( result ) );
    TEST_ASSERT( flags == ( flags_result ) );
119
}
120
/* END_CASE */
121

122
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_USE_C */
123
void x509_dn_gets( char *crt_file, char *entity, char *result_str )
124 125 126
{
    x509_cert   crt;
    char buf[2000];
127
    int res = 0;
128 129 130 131

    memset( &crt, 0, sizeof( x509_cert ) );
    memset( buf, 0, 2000 );

132 133
    TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
    if( strcmp( entity, "subject" ) == 0 )
134
        res =  x509parse_dn_gets( buf, 2000, &crt.subject );
135
    else if( strcmp( entity, "issuer" ) == 0 )
136 137 138
        res =  x509parse_dn_gets( buf, 2000, &crt.issuer );
    else
        TEST_ASSERT( "Unknown entity" == 0 );
139

140
    x509_crt_free( &crt );
Paul Bakker's avatar
Paul Bakker committed
141

142 143 144
    TEST_ASSERT( res != -1 );
    TEST_ASSERT( res != -2 );

145
    TEST_ASSERT( strcmp( buf, result_str ) == 0 );
146
}
147
/* END_CASE */
148

149
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_USE_C */
150
void x509_time_expired( char *crt_file, char *entity, int result )
151 152 153 154 155
{
    x509_cert   crt;

    memset( &crt, 0, sizeof( x509_cert ) );

156
    TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
157

158 159 160 161
    if( strcmp( entity, "valid_from" ) == 0 )
        TEST_ASSERT( x509parse_time_expired( &crt.valid_from ) == result );
    else if( strcmp( entity, "valid_to" ) == 0 )
        TEST_ASSERT( x509parse_time_expired( &crt.valid_to ) == result );
162 163
    else
        TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker's avatar
Paul Bakker committed
164

165
    x509_crt_free( &crt );
166
}
167
/* END_CASE */
168

169
/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
170
void x509parse_crt( char *crt_data, char *result_str, int result )
171 172 173 174 175 176 177 178 179 180
{
    x509_cert   crt;
    unsigned char buf[2000];
    unsigned char output[2000];
    int data_len, res;

    memset( &crt, 0, sizeof( x509_cert ) );
    memset( buf, 0, 2000 );
    memset( output, 0, 2000 );

181
    data_len = unhexify( buf, crt_data );
182

183 184
    TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( result ) );
    if( ( result ) == 0 )
185 186
    {
        res = x509parse_cert_info( (char *) output, 2000, "", &crt );
187

188 189 190
        TEST_ASSERT( res != -1 );
        TEST_ASSERT( res != -2 );

191
        TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
192
    }
Paul Bakker's avatar
Paul Bakker committed
193

194
    x509_crt_free( &crt );
195
}
196
/* END_CASE */
197

198
/* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */
199
void x509parse_crl( char *crl_data, char *result_str, int result )
200 201 202 203 204 205 206 207 208 209
{
    x509_crl   crl;
    unsigned char buf[2000];
    unsigned char output[2000];
    int data_len, res;

    memset( &crl, 0, sizeof( x509_crl ) );
    memset( buf, 0, 2000 );
    memset( output, 0, 2000 );

210
    data_len = unhexify( buf, crl_data );
211

212 213
    TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( result ) );
    if( ( result ) == 0 )
214 215
    {
        res = x509parse_crl_info( (char *) output, 2000, "", &crl );
216

217 218 219
        TEST_ASSERT( res != -1 );
        TEST_ASSERT( res != -2 );

220
        TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
221
    }
Paul Bakker's avatar
Paul Bakker committed
222 223

    x509_crl_free( &crl );
224
}
225
/* END_CASE */
226

227
/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
228
void x509_selftest()
229 230 231
{
    TEST_ASSERT( x509_self_test( 0 ) == 0 );
}
232
/* END_CASE */