test_suite_x509parse.function 7.89 KB
Newer Older
1
/* BEGIN_HEADER */
2
#include <polarssl/x509.h>
3
#include <polarssl/pem.h>
4
#include <polarssl/oid.h>
5

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

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

23 24 25
    return 0;
}

26
/* END_HEADER */
27

28 29 30 31
/* BEGIN_DEPENDENCIES
 * depends_on:POLARSSL_X509_PARSE_C:POLARSSL_BIGNUM_C
 * END_DEPENDENCIES
 */
32

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

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

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

Paul Bakker's avatar
Paul Bakker committed
46 47
    x509_free( &crt );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Paul Bakker's avatar
Paul Bakker committed
139 140
    x509_free( &crt );

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

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

148 149
/* BEGIN_CASE */
void x509_time_expired( char *crt_file, char *entity, int result )
150 151 152 153 154
{
    x509_cert   crt;

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

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

157 158 159 160
    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 );
161 162
    else
        TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker's avatar
Paul Bakker committed
163 164

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

168 169
/* BEGIN_CASE */
void x509parse_keyfile_rsa( char *key_file, char *password, int result )
170 171
{
    rsa_context rsa;
172
    int res;
173
    char *pwd = password;
174 175 176

    memset( &rsa, 0, sizeof( rsa_context ) );

177 178 179
    if( strcmp( pwd, "NULL" ) == 0 )
        pwd = NULL;

180
    res = x509parse_keyfile_rsa( &rsa, key_file, pwd );
181

182
    TEST_ASSERT( res == result );
183 184 185 186 187

    if( res == 0 )
    {
        TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 );
    }
Paul Bakker's avatar
Paul Bakker committed
188 189

    rsa_free( &rsa );
190
}
191
/* END_CASE */
192

193 194
/* BEGIN_CASE */
void x509parse_public_keyfile_rsa( char *key_file, int result )
195 196 197 198 199 200
{
    rsa_context rsa;
    int res;

    memset( &rsa, 0, sizeof( rsa_context ) );

201
    res = x509parse_public_keyfile_rsa( &rsa, key_file );
202

203
    TEST_ASSERT( res == result );
204 205 206 207 208

    if( res == 0 )
    {
        TEST_ASSERT( rsa_check_pubkey( &rsa ) == 0 );
    }
Paul Bakker's avatar
Paul Bakker committed
209 210

    rsa_free( &rsa );
211
}
212
/* END_CASE */
213

214 215
/* BEGIN_CASE */
void x509parse_public_keyfile_ec( char *key_file, int result )
216
{
217
    pk_context ctx;
218 219
    int res;

220
    pk_init( &ctx );
221

222
    res = x509parse_public_keyfile( &ctx, key_file );
223

224
    TEST_ASSERT( res == result );
225 226 227

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

234
    pk_free( &ctx );
235
}
236
/* END_CASE */
237

238 239
/* BEGIN_CASE */
void x509parse_keyfile_ec( char *key_file, char *password, int result )
240
{
241
    pk_context ctx;
242 243
    int res;

244
    pk_init( &ctx );
245

246
    res = x509parse_keyfile( &ctx, key_file, password );
247

248
    TEST_ASSERT( res == result );
249 250 251

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

258
    pk_free( &ctx );
259
}
260
/* END_CASE */
261

262 263
/* BEGIN_CASE */
void x509parse_crt( char *crt_data, char *result_str, int result )
264 265 266 267 268 269 270 271 272 273
{
    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 );

274
    data_len = unhexify( buf, crt_data );
275

276 277
    TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( result ) );
    if( ( result ) == 0 )
278 279
    {
        res = x509parse_cert_info( (char *) output, 2000, "", &crt );
280

281 282 283
        TEST_ASSERT( res != -1 );
        TEST_ASSERT( res != -2 );

284
        TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
285
    }
Paul Bakker's avatar
Paul Bakker committed
286 287

    x509_free( &crt );
288
}
289
/* END_CASE */
290

291 292
/* BEGIN_CASE */
void x509parse_crl( char *crl_data, char *result_str, int result )
293 294 295 296 297 298 299 300 301 302
{
    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 );

303
    data_len = unhexify( buf, crl_data );
304

305 306
    TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( result ) );
    if( ( result ) == 0 )
307 308
    {
        res = x509parse_crl_info( (char *) output, 2000, "", &crl );
309

310 311 312
        TEST_ASSERT( res != -1 );
        TEST_ASSERT( res != -2 );

313
        TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
314
    }
Paul Bakker's avatar
Paul Bakker committed
315 316

    x509_crl_free( &crl );
317
}
318
/* END_CASE */
319

320 321
/* BEGIN_CASE */
void x509parse_key_rsa( char *key_data, char *result_str, int result )
322 323 324 325
{
    rsa_context   rsa;
    unsigned char buf[2000];
    unsigned char output[2000];
326
    int data_len;
327
    ((void) result_str);
328 329 330 331 332

    memset( &rsa, 0, sizeof( rsa_context ) );
    memset( buf, 0, 2000 );
    memset( output, 0, 2000 );

333
    data_len = unhexify( buf, key_data );
334

335 336
    TEST_ASSERT( x509parse_key_rsa( &rsa, buf, data_len, NULL, 0 ) == ( result ) );
    if( ( result ) == 0 )
337 338 339
    {
        TEST_ASSERT( 1 );
    }
Paul Bakker's avatar
Paul Bakker committed
340 341

    rsa_free( &rsa );
342
}
343
/* END_CASE */
344

345 346
/* BEGIN_CASE */
void x509_selftest()
347 348 349
{
    TEST_ASSERT( x509_self_test( 0 ) == 0 );
}
350
/* END_CASE */