00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_BIGNUM_C
00004
00005 #include <polarssl/x509_crt.h>
00006 #include <polarssl/x509_crl.h>
00007 #include <polarssl/pem.h>
00008 #include <polarssl/oid.h>
00009
00010 int verify_none( void *data, x509_crt *crt, int certificate_depth, int *flags )
00011 {
00012 ((void) data);
00013 ((void) crt);
00014 ((void) certificate_depth);
00015 *flags |= BADCERT_OTHER;
00016
00017 return 0;
00018 }
00019
00020 int verify_all( void *data, x509_crt *crt, int certificate_depth, int *flags )
00021 {
00022 ((void) data);
00023 ((void) crt);
00024 ((void) certificate_depth);
00025 *flags = 0;
00026
00027 return 0;
00028 }
00029
00030 #endif
00031
00032
00033 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00034 #include "polarssl/memory.h"
00035 #endif
00036
00037 #if defined(WANT_NOT_RND_MPI)
00038 #if defined(POLARSSL_BIGNUM_C)
00039 #include "polarssl/bignum.h"
00040 #else
00041 #error "not_rnd_mpi() need bignum.c"
00042 #endif
00043 #endif
00044
00045 #ifdef _MSC_VER
00046 #include <basetsd.h>
00047 typedef UINT32 uint32_t;
00048 #else
00049 #include <inttypes.h>
00050 #endif
00051
00052 #include <assert.h>
00053 #include <stdlib.h>
00054 #include <string.h>
00055
00056
00057
00058
00059 #ifndef GET_UINT32_BE
00060 #define GET_UINT32_BE(n,b,i) \
00061 { \
00062 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00063 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00064 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00065 | ( (uint32_t) (b)[(i) + 3] ); \
00066 }
00067 #endif
00068
00069 #ifndef PUT_UINT32_BE
00070 #define PUT_UINT32_BE(n,b,i) \
00071 { \
00072 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00073 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00074 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00075 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00076 }
00077 #endif
00078
00079 static int unhexify(unsigned char *obuf, const char *ibuf)
00080 {
00081 unsigned char c, c2;
00082 int len = strlen(ibuf) / 2;
00083 assert(!(strlen(ibuf) %1));
00084
00085 while (*ibuf != 0)
00086 {
00087 c = *ibuf++;
00088 if( c >= '0' && c <= '9' )
00089 c -= '0';
00090 else if( c >= 'a' && c <= 'f' )
00091 c -= 'a' - 10;
00092 else if( c >= 'A' && c <= 'F' )
00093 c -= 'A' - 10;
00094 else
00095 assert( 0 );
00096
00097 c2 = *ibuf++;
00098 if( c2 >= '0' && c2 <= '9' )
00099 c2 -= '0';
00100 else if( c2 >= 'a' && c2 <= 'f' )
00101 c2 -= 'a' - 10;
00102 else if( c2 >= 'A' && c2 <= 'F' )
00103 c2 -= 'A' - 10;
00104 else
00105 assert( 0 );
00106
00107 *obuf++ = ( c << 4 ) | c2;
00108 }
00109
00110 return len;
00111 }
00112
00113 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00114 {
00115 unsigned char l, h;
00116
00117 while (len != 0)
00118 {
00119 h = (*ibuf) / 16;
00120 l = (*ibuf) % 16;
00121
00122 if( h < 10 )
00123 *obuf++ = '0' + h;
00124 else
00125 *obuf++ = 'a' + h - 10;
00126
00127 if( l < 10 )
00128 *obuf++ = '0' + l;
00129 else
00130 *obuf++ = 'a' + l - 10;
00131
00132 ++ibuf;
00133 len--;
00134 }
00135 }
00136
00146 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00147 {
00148 size_t i;
00149
00150 if( rng_state != NULL )
00151 rng_state = NULL;
00152
00153 for( i = 0; i < len; ++i )
00154 output[i] = rand();
00155
00156 return( 0 );
00157 }
00158
00164 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00165 {
00166 if( rng_state != NULL )
00167 rng_state = NULL;
00168
00169 memset( output, 0, len );
00170
00171 return( 0 );
00172 }
00173
00174 typedef struct
00175 {
00176 unsigned char *buf;
00177 size_t length;
00178 } rnd_buf_info;
00179
00191 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00192 {
00193 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00194 size_t use_len;
00195
00196 if( rng_state == NULL )
00197 return( rnd_std_rand( NULL, output, len ) );
00198
00199 use_len = len;
00200 if( len > info->length )
00201 use_len = info->length;
00202
00203 if( use_len )
00204 {
00205 memcpy( output, info->buf, use_len );
00206 info->buf += use_len;
00207 info->length -= use_len;
00208 }
00209
00210 if( len - use_len > 0 )
00211 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00212
00213 return( 0 );
00214 }
00215
00223 typedef struct
00224 {
00225 uint32_t key[16];
00226 uint32_t v0, v1;
00227 } rnd_pseudo_info;
00228
00237 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00238 {
00239 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00240 uint32_t i, *k, sum, delta=0x9E3779B9;
00241 unsigned char result[4];
00242
00243 if( rng_state == NULL )
00244 return( rnd_std_rand( NULL, output, len ) );
00245
00246 k = info->key;
00247
00248 while( len > 0 )
00249 {
00250 size_t use_len = ( len > 4 ) ? 4 : len;
00251 sum = 0;
00252
00253 for( i = 0; i < 32; i++ )
00254 {
00255 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00256 sum += delta;
00257 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00258 }
00259
00260 PUT_UINT32_BE( info->v0, result, 0 );
00261 memcpy( output, result, use_len );
00262 len -= use_len;
00263 }
00264
00265 return( 0 );
00266 }
00267
00268 #if defined(WANT_NOT_RND_MPI)
00269
00277 #define ciL (sizeof(t_uint))
00278 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00279 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00280 {
00281 char *str = (char *) in;
00282 mpi X;
00283
00284
00285
00286
00287
00288 X.s = 1;
00289 X.p = (t_uint *) out;
00290 X.n = CHARS_TO_LIMBS( len );
00291
00292
00293
00294
00295
00296 assert( strlen( str ) / 2 == len );
00297
00298 return( mpi_read_string( &X, 16, str ) );
00299 }
00300 #endif
00301
00302
00303 #include <stdio.h>
00304 #include <string.h>
00305
00306 static int test_errors = 0;
00307
00308 #ifdef POLARSSL_BIGNUM_C
00309
00310 #define TEST_SUITE_ACTIVE
00311
00312 static int test_assert( int correct, char *test )
00313 {
00314 if( correct )
00315 return( 0 );
00316
00317 test_errors++;
00318 if( test_errors == 1 )
00319 printf( "FAILED\n" );
00320 printf( " %s\n", test );
00321
00322 return( 1 );
00323 }
00324
00325 #define TEST_ASSERT( TEST ) \
00326 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00327 if( test_errors) return; \
00328 } while (0)
00329
00330 int verify_string( char **str )
00331 {
00332 if( (*str)[0] != '"' ||
00333 (*str)[strlen( *str ) - 1] != '"' )
00334 {
00335 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00336 return( -1 );
00337 }
00338
00339 (*str)++;
00340 (*str)[strlen( *str ) - 1] = '\0';
00341
00342 return( 0 );
00343 }
00344
00345 int verify_int( char *str, int *value )
00346 {
00347 size_t i;
00348 int minus = 0;
00349 int digits = 1;
00350 int hex = 0;
00351
00352 for( i = 0; i < strlen( str ); i++ )
00353 {
00354 if( i == 0 && str[i] == '-' )
00355 {
00356 minus = 1;
00357 continue;
00358 }
00359
00360 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00361 str[i - 1] == '0' && str[i] == 'x' )
00362 {
00363 hex = 1;
00364 continue;
00365 }
00366
00367 if( str[i] < '0' || str[i] > '9' )
00368 {
00369 digits = 0;
00370 break;
00371 }
00372 }
00373
00374 if( digits )
00375 {
00376 if( hex )
00377 *value = strtol( str, NULL, 16 );
00378 else
00379 *value = strtol( str, NULL, 10 );
00380
00381 return( 0 );
00382 }
00383
00384 #ifdef POLARSSL_X509_CRT_PARSE_C
00385 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
00386 {
00387 *value = ( POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
00388 return( 0 );
00389 }
00390 #endif // POLARSSL_X509_CRT_PARSE_C
00391 #ifdef POLARSSL_X509_CRT_PARSE_C
00392 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00393 {
00394 *value = ( POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00395 return( 0 );
00396 }
00397 #endif // POLARSSL_X509_CRT_PARSE_C
00398 #ifdef POLARSSL_X509_CRL_PARSE_C
00399 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00400 {
00401 *value = ( POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00402 return( 0 );
00403 }
00404 #endif // POLARSSL_X509_CRL_PARSE_C
00405 #ifdef POLARSSL_FS_IO
00406 #ifdef POLARSSL_X509_CRT_PARSE_C
00407 #ifdef POLARSSL_X509_CRL_PARSE_C
00408 if( strcmp( str, "POLARSSL_ERR_X509_CERT_VERIFY_FAILED" ) == 0 )
00409 {
00410 *value = ( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
00411 return( 0 );
00412 }
00413 #endif // POLARSSL_FS_IO
00414 #endif // POLARSSL_X509_CRT_PARSE_C
00415 #endif // POLARSSL_X509_CRL_PARSE_C
00416 #ifdef POLARSSL_X509_CRT_PARSE_C
00417 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00418 {
00419 *value = ( POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00420 return( 0 );
00421 }
00422 #endif // POLARSSL_X509_CRT_PARSE_C
00423 #ifdef POLARSSL_X509_CRT_PARSE_C
00424 if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_VERSION" ) == 0 )
00425 {
00426 *value = ( POLARSSL_ERR_X509_UNKNOWN_VERSION );
00427 return( 0 );
00428 }
00429 #endif // POLARSSL_X509_CRT_PARSE_C
00430 #ifdef POLARSSL_X509_CRL_PARSE_C
00431 if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_VERSION" ) == 0 )
00432 {
00433 *value = ( POLARSSL_ERR_X509_UNKNOWN_VERSION );
00434 return( 0 );
00435 }
00436 #endif // POLARSSL_X509_CRL_PARSE_C
00437 #ifdef POLARSSL_X509_CRT_PARSE_C
00438 if( strcmp( str, "POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
00439 {
00440 *value = ( POLARSSL_ERR_ASN1_INVALID_LENGTH );
00441 return( 0 );
00442 }
00443 #endif // POLARSSL_X509_CRT_PARSE_C
00444 #ifdef POLARSSL_X509_CRT_PARSE_C
00445 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00446 {
00447 *value = ( POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00448 return( 0 );
00449 }
00450 #endif // POLARSSL_X509_CRT_PARSE_C
00451 #ifdef POLARSSL_X509_CRL_PARSE_C
00452 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00453 {
00454 *value = ( POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00455 return( 0 );
00456 }
00457 #endif // POLARSSL_X509_CRL_PARSE_C
00458 #ifdef POLARSSL_FS_IO
00459 #ifdef POLARSSL_X509_CRT_PARSE_C
00460 #ifdef POLARSSL_X509_CRL_PARSE_C
00461 if( strcmp( str, "BADCERT_REVOKED | BADCERT_CN_MISMATCH" ) == 0 )
00462 {
00463 *value = ( BADCERT_REVOKED | BADCERT_CN_MISMATCH );
00464 return( 0 );
00465 }
00466 #endif // POLARSSL_FS_IO
00467 #endif // POLARSSL_X509_CRT_PARSE_C
00468 #endif // POLARSSL_X509_CRL_PARSE_C
00469 #ifdef POLARSSL_X509_CRT_PARSE_C
00470 if( strcmp( str, "POLARSSL_ERR_PK_UNKNOWN_PK_ALG" ) == 0 )
00471 {
00472 *value = ( POLARSSL_ERR_PK_UNKNOWN_PK_ALG );
00473 return( 0 );
00474 }
00475 #endif // POLARSSL_X509_CRT_PARSE_C
00476 #ifdef POLARSSL_X509_CRT_PARSE_C
00477 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
00478 {
00479 *value = ( POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
00480 return( 0 );
00481 }
00482 #endif // POLARSSL_X509_CRT_PARSE_C
00483 #ifdef POLARSSL_X509_CRT_PARSE_C
00484 if( strcmp( str, "POLARSSL_ERR_X509_SIG_MISMATCH" ) == 0 )
00485 {
00486 *value = ( POLARSSL_ERR_X509_SIG_MISMATCH );
00487 return( 0 );
00488 }
00489 #endif // POLARSSL_X509_CRT_PARSE_C
00490 #ifdef POLARSSL_X509_CRL_PARSE_C
00491 if( strcmp( str, "POLARSSL_ERR_X509_SIG_MISMATCH" ) == 0 )
00492 {
00493 *value = ( POLARSSL_ERR_X509_SIG_MISMATCH );
00494 return( 0 );
00495 }
00496 #endif // POLARSSL_X509_CRL_PARSE_C
00497 #ifdef POLARSSL_X509_CRT_PARSE_C
00498 if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
00499 {
00500 *value = ( POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
00501 return( 0 );
00502 }
00503 #endif // POLARSSL_X509_CRT_PARSE_C
00504 #ifdef POLARSSL_X509_CRT_PARSE_C
00505 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00506 {
00507 *value = ( POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00508 return( 0 );
00509 }
00510 #endif // POLARSSL_X509_CRT_PARSE_C
00511 #ifdef POLARSSL_X509_CRL_PARSE_C
00512 if( strcmp( str, "POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00513 {
00514 *value = ( POLARSSL_ERR_ASN1_OUT_OF_DATA );
00515 return( 0 );
00516 }
00517 #endif // POLARSSL_X509_CRL_PARSE_C
00518 #ifdef POLARSSL_X509_CRT_PARSE_C
00519 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA " ) == 0 )
00520 {
00521 *value = ( POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00522 return( 0 );
00523 }
00524 #endif // POLARSSL_X509_CRT_PARSE_C
00525 #ifdef POLARSSL_FS_IO
00526 #ifdef POLARSSL_X509_CRT_PARSE_C
00527 #ifdef POLARSSL_X509_CRL_PARSE_C
00528 if( strcmp( str, "BADCERT_CN_MISMATCH" ) == 0 )
00529 {
00530 *value = ( BADCERT_CN_MISMATCH );
00531 return( 0 );
00532 }
00533 #endif // POLARSSL_FS_IO
00534 #endif // POLARSSL_X509_CRT_PARSE_C
00535 #endif // POLARSSL_X509_CRL_PARSE_C
00536 #ifdef POLARSSL_X509_CRT_PARSE_C
00537 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00538 {
00539 *value = ( POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00540 return( 0 );
00541 }
00542 #endif // POLARSSL_X509_CRT_PARSE_C
00543 #ifdef POLARSSL_X509_CRT_PARSE_C
00544 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
00545 {
00546 *value = ( POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_INVALID_LENGTH );
00547 return( 0 );
00548 }
00549 #endif // POLARSSL_X509_CRT_PARSE_C
00550 #ifdef POLARSSL_FS_IO
00551 #ifdef POLARSSL_X509_CRT_PARSE_C
00552 #ifdef POLARSSL_X509_CRL_PARSE_C
00553 if( strcmp( str, "BADCERT_NOT_TRUSTED" ) == 0 )
00554 {
00555 *value = ( BADCERT_NOT_TRUSTED );
00556 return( 0 );
00557 }
00558 #endif // POLARSSL_FS_IO
00559 #endif // POLARSSL_X509_CRT_PARSE_C
00560 #endif // POLARSSL_X509_CRL_PARSE_C
00561 #ifdef POLARSSL_X509_CRT_PARSE_C
00562 if( strcmp( str, "POLARSSL_ERR_PK_KEY_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00563 {
00564 *value = ( POLARSSL_ERR_PK_KEY_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00565 return( 0 );
00566 }
00567 #endif // POLARSSL_X509_CRT_PARSE_C
00568 #ifdef POLARSSL_X509_CRT_PARSE_C
00569 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00570 {
00571 *value = ( POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00572 return( 0 );
00573 }
00574 #endif // POLARSSL_X509_CRT_PARSE_C
00575 #ifdef POLARSSL_X509_CRT_PARSE_C
00576 if( strcmp( str, " 1" ) == 0 )
00577 {
00578 *value = ( 1 );
00579 return( 0 );
00580 }
00581 #endif // POLARSSL_X509_CRT_PARSE_C
00582 #ifdef POLARSSL_X509_CRL_PARSE_C
00583 if( strcmp( str, " 1" ) == 0 )
00584 {
00585 *value = ( 1 );
00586 return( 0 );
00587 }
00588 #endif // POLARSSL_X509_CRL_PARSE_C
00589 #ifdef POLARSSL_FS_IO
00590 #ifdef POLARSSL_X509_CRT_PARSE_C
00591 #ifdef POLARSSL_X509_CRL_PARSE_C
00592 if( strcmp( str, "BADCRL_EXPIRED" ) == 0 )
00593 {
00594 *value = ( BADCRL_EXPIRED );
00595 return( 0 );
00596 }
00597 #endif // POLARSSL_FS_IO
00598 #endif // POLARSSL_X509_CRT_PARSE_C
00599 #endif // POLARSSL_X509_CRL_PARSE_C
00600 #ifdef POLARSSL_X509_CRT_PARSE_C
00601 if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00602 {
00603 *value = ( POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00604 return( 0 );
00605 }
00606 #endif // POLARSSL_X509_CRT_PARSE_C
00607 #ifdef POLARSSL_X509_CRT_PARSE_C
00608 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00609 {
00610 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00611 return( 0 );
00612 }
00613 #endif // POLARSSL_X509_CRT_PARSE_C
00614 #ifdef POLARSSL_X509_CRL_PARSE_C
00615 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00616 {
00617 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00618 return( 0 );
00619 }
00620 #endif // POLARSSL_X509_CRL_PARSE_C
00621 #ifdef POLARSSL_FS_IO
00622 #ifdef POLARSSL_X509_CRT_PARSE_C
00623 #ifdef POLARSSL_X509_CRL_PARSE_C
00624 if( strcmp( str, "BADCERT_REVOKED | BADCRL_EXPIRED" ) == 0 )
00625 {
00626 *value = ( BADCERT_REVOKED | BADCRL_EXPIRED );
00627 return( 0 );
00628 }
00629 #endif // POLARSSL_FS_IO
00630 #endif // POLARSSL_X509_CRT_PARSE_C
00631 #endif // POLARSSL_X509_CRL_PARSE_C
00632 #ifdef POLARSSL_X509_CRT_PARSE_C
00633 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00634 {
00635 *value = ( POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00636 return( 0 );
00637 }
00638 #endif // POLARSSL_X509_CRT_PARSE_C
00639 #ifdef POLARSSL_X509_CRL_PARSE_C
00640 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00641 {
00642 *value = ( POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00643 return( 0 );
00644 }
00645 #endif // POLARSSL_X509_CRL_PARSE_C
00646 #ifdef POLARSSL_FS_IO
00647 #ifdef POLARSSL_X509_CRT_PARSE_C
00648 #ifdef POLARSSL_X509_CRL_PARSE_C
00649 if( strcmp( str, "BADCERT_REVOKED" ) == 0 )
00650 {
00651 *value = ( BADCERT_REVOKED );
00652 return( 0 );
00653 }
00654 #endif // POLARSSL_FS_IO
00655 #endif // POLARSSL_X509_CRT_PARSE_C
00656 #endif // POLARSSL_X509_CRL_PARSE_C
00657 #ifdef POLARSSL_X509_CRT_PARSE_C
00658 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00659 {
00660 *value = ( POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00661 return( 0 );
00662 }
00663 #endif // POLARSSL_X509_CRT_PARSE_C
00664 #ifdef POLARSSL_X509_CRT_PARSE_C
00665 if( strcmp( str, "POLARSSL_ERR_PK_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00666 {
00667 *value = ( POLARSSL_ERR_PK_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00668 return( 0 );
00669 }
00670 #endif // POLARSSL_X509_CRT_PARSE_C
00671 #ifdef POLARSSL_X509_CRT_PARSE_C
00672 if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + POLARSSL_ERR_OID_NOT_FOUND" ) == 0 )
00673 {
00674 *value = ( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + POLARSSL_ERR_OID_NOT_FOUND );
00675 return( 0 );
00676 }
00677 #endif // POLARSSL_X509_CRT_PARSE_C
00678 #ifdef POLARSSL_X509_CRT_PARSE_C
00679 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_INVALID_DATA" ) == 0 )
00680 {
00681 *value = ( POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_INVALID_DATA );
00682 return( 0 );
00683 }
00684 #endif // POLARSSL_X509_CRT_PARSE_C
00685 #ifdef POLARSSL_X509_CRT_PARSE_C
00686 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
00687 {
00688 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
00689 return( 0 );
00690 }
00691 #endif // POLARSSL_X509_CRT_PARSE_C
00692 #ifdef POLARSSL_X509_CRL_PARSE_C
00693 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
00694 {
00695 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
00696 return( 0 );
00697 }
00698 #endif // POLARSSL_X509_CRL_PARSE_C
00699 #ifdef POLARSSL_X509_CRL_PARSE_C
00700 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00701 {
00702 *value = ( POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00703 return( 0 );
00704 }
00705 #endif // POLARSSL_X509_CRL_PARSE_C
00706 #ifdef POLARSSL_X509_CRT_PARSE_C
00707 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00708 {
00709 *value = ( POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00710 return( 0 );
00711 }
00712 #endif // POLARSSL_X509_CRT_PARSE_C
00713 #ifdef POLARSSL_X509_CRL_PARSE_C
00714 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00715 {
00716 *value = ( POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00717 return( 0 );
00718 }
00719 #endif // POLARSSL_X509_CRL_PARSE_C
00720 #ifdef POLARSSL_X509_CRT_PARSE_C
00721 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00722 {
00723 *value = ( POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00724 return( 0 );
00725 }
00726 #endif // POLARSSL_X509_CRT_PARSE_C
00727 #ifdef POLARSSL_X509_CRT_PARSE_C
00728 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE" ) == 0 )
00729 {
00730 *value = ( POLARSSL_ERR_X509_INVALID_DATE );
00731 return( 0 );
00732 }
00733 #endif // POLARSSL_X509_CRT_PARSE_C
00734 #ifdef POLARSSL_X509_CRT_PARSE_C
00735 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00736 {
00737 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00738 return( 0 );
00739 }
00740 #endif // POLARSSL_X509_CRT_PARSE_C
00741 #ifdef POLARSSL_X509_CRT_PARSE_C
00742 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
00743 {
00744 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_INVALID_LENGTH );
00745 return( 0 );
00746 }
00747 #endif // POLARSSL_X509_CRT_PARSE_C
00748 #ifdef POLARSSL_FS_IO
00749 #ifdef POLARSSL_X509_CRT_PARSE_C
00750 #ifdef POLARSSL_X509_CRL_PARSE_C
00751 if( strcmp( str, "BADCERT_OTHER" ) == 0 )
00752 {
00753 *value = ( BADCERT_OTHER );
00754 return( 0 );
00755 }
00756 #endif // POLARSSL_FS_IO
00757 #endif // POLARSSL_X509_CRT_PARSE_C
00758 #endif // POLARSSL_X509_CRL_PARSE_C
00759 #ifdef POLARSSL_X509_CRT_PARSE_C
00760 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT" ) == 0 )
00761 {
00762 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT );
00763 return( 0 );
00764 }
00765 #endif // POLARSSL_X509_CRT_PARSE_C
00766 #ifdef POLARSSL_X509_CRL_PARSE_C
00767 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT" ) == 0 )
00768 {
00769 *value = ( POLARSSL_ERR_X509_INVALID_FORMAT );
00770 return( 0 );
00771 }
00772 #endif // POLARSSL_X509_CRL_PARSE_C
00773 #ifdef POLARSSL_X509_CRT_PARSE_C
00774 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00775 {
00776 *value = ( POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00777 return( 0 );
00778 }
00779 #endif // POLARSSL_X509_CRT_PARSE_C
00780 #ifdef POLARSSL_X509_CRT_PARSE_C
00781 if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_INVALID_DATA" ) == 0 )
00782 {
00783 *value = ( POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_INVALID_DATA );
00784 return( 0 );
00785 }
00786 #endif // POLARSSL_X509_CRT_PARSE_C
00787 #ifdef POLARSSL_X509_CRT_PARSE_C
00788 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
00789 {
00790 *value = ( POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
00791 return( 0 );
00792 }
00793 #endif // POLARSSL_X509_CRT_PARSE_C
00794 #ifdef POLARSSL_X509_CRT_PARSE_C
00795 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
00796 {
00797 *value = ( POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
00798 return( 0 );
00799 }
00800 #endif // POLARSSL_X509_CRT_PARSE_C
00801 #ifdef POLARSSL_FS_IO
00802 #ifdef POLARSSL_X509_CRT_PARSE_C
00803 #ifdef POLARSSL_X509_CRL_PARSE_C
00804 if( strcmp( str, "BADCERT_REVOKED | BADCRL_EXPIRED | BADCERT_CN_MISMATCH" ) == 0 )
00805 {
00806 *value = ( BADCERT_REVOKED | BADCRL_EXPIRED | BADCERT_CN_MISMATCH );
00807 return( 0 );
00808 }
00809 #endif // POLARSSL_FS_IO
00810 #endif // POLARSSL_X509_CRT_PARSE_C
00811 #endif // POLARSSL_X509_CRL_PARSE_C
00812 #ifdef POLARSSL_X509_CRT_PARSE_C
00813 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00814 {
00815 *value = ( POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00816 return( 0 );
00817 }
00818 #endif // POLARSSL_X509_CRT_PARSE_C
00819 #ifdef POLARSSL_X509_CRL_PARSE_C
00820 if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
00821 {
00822 *value = ( POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_OUT_OF_DATA );
00823 return( 0 );
00824 }
00825 #endif // POLARSSL_X509_CRL_PARSE_C
00826 #ifdef POLARSSL_X509_CRL_PARSE_C
00827 if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_SIG_ALG" ) == 0 )
00828 {
00829 *value = ( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG );
00830 return( 0 );
00831 }
00832 #endif // POLARSSL_X509_CRL_PARSE_C
00833 #ifdef POLARSSL_X509_CRT_PARSE_C
00834 if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
00835 {
00836 *value = ( POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
00837 return( 0 );
00838 }
00839 #endif // POLARSSL_X509_CRT_PARSE_C
00840 #ifdef POLARSSL_FS_IO
00841 #ifdef POLARSSL_X509_CRT_PARSE_C
00842 #ifdef POLARSSL_X509_CRL_PARSE_C
00843 if( strcmp( str, "BADCERT_CN_MISMATCH + BADCERT_NOT_TRUSTED" ) == 0 )
00844 {
00845 *value = ( BADCERT_CN_MISMATCH + BADCERT_NOT_TRUSTED );
00846 return( 0 );
00847 }
00848 #endif // POLARSSL_FS_IO
00849 #endif // POLARSSL_X509_CRT_PARSE_C
00850 #endif // POLARSSL_X509_CRL_PARSE_C
00851 #ifdef POLARSSL_X509_CRT_PARSE_C
00852 if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY" ) == 0 )
00853 {
00854 *value = ( POLARSSL_ERR_PK_INVALID_PUBKEY );
00855 return( 0 );
00856 }
00857 #endif // POLARSSL_X509_CRT_PARSE_C
00858
00859
00860 printf( "Expected integer for parameter and got: %s\n", str );
00861 return( -1 );
00862 }
00863
00864 #ifdef POLARSSL_FS_IO
00865 #ifdef POLARSSL_X509_CRT_PARSE_C
00866 void test_suite_x509_cert_info( char *crt_file, char *result_str )
00867 {
00868 x509_crt crt;
00869 char buf[2000];
00870 int res;
00871
00872 x509_crt_init( &crt );
00873 memset( buf, 0, 2000 );
00874
00875 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
00876 res = x509_crt_info( buf, 2000, "", &crt );
00877
00878 x509_crt_free( &crt );
00879
00880 TEST_ASSERT( res != -1 );
00881 TEST_ASSERT( res != -2 );
00882
00883 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
00884 }
00885 #endif
00886 #endif
00887
00888 #ifdef POLARSSL_FS_IO
00889 #ifdef POLARSSL_X509_CRL_PARSE_C
00890 void test_suite_x509_crl_info( char *crl_file, char *result_str )
00891 {
00892 x509_crl crl;
00893 char buf[2000];
00894 int res;
00895
00896 x509_crl_init( &crl );
00897 memset( buf, 0, 2000 );
00898
00899 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
00900 res = x509_crl_info( buf, 2000, "", &crl );
00901
00902 x509_crl_free( &crl );
00903
00904 TEST_ASSERT( res != -1 );
00905 TEST_ASSERT( res != -2 );
00906
00907 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
00908 }
00909 #endif
00910 #endif
00911
00912 #ifdef POLARSSL_FS_IO
00913 #ifdef POLARSSL_X509_CRT_PARSE_C
00914 #ifdef POLARSSL_X509_CRL_PARSE_C
00915 void test_suite_x509_verify( char *crt_file, char *ca_file, char *crl_file,
00916 char *cn_name_str, int result, int flags_result,
00917 char *verify_callback )
00918 {
00919 x509_crt crt;
00920 x509_crt ca;
00921 x509_crl crl;
00922 int flags = 0;
00923 int res;
00924 int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
00925 char * cn_name = NULL;
00926
00927 x509_crt_init( &crt );
00928 x509_crt_init( &ca );
00929 x509_crl_init( &crl );
00930
00931 if( strcmp( cn_name_str, "NULL" ) != 0 )
00932 cn_name = cn_name_str;
00933
00934 if( strcmp( verify_callback, "NULL" ) == 0 )
00935 f_vrfy = NULL;
00936 else if( strcmp( verify_callback, "verify_none" ) == 0 )
00937 f_vrfy = verify_none;
00938 else if( strcmp( verify_callback, "verify_all" ) == 0 )
00939 f_vrfy = verify_all;
00940 else
00941 TEST_ASSERT( "No known verify callback selected" == 0 );
00942
00943 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
00944 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
00945 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
00946
00947 res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
00948
00949 x509_crt_free( &crt );
00950 x509_crt_free( &ca );
00951 x509_crl_free( &crl );
00952
00953 TEST_ASSERT( res == ( result ) );
00954 TEST_ASSERT( flags == ( flags_result ) );
00955 }
00956 #endif
00957 #endif
00958 #endif
00959
00960 #ifdef POLARSSL_FS_IO
00961 #ifdef POLARSSL_X509_USE_C
00962 void test_suite_x509_dn_gets( char *crt_file, char *entity, char *result_str )
00963 {
00964 x509_crt crt;
00965 char buf[2000];
00966 int res = 0;
00967
00968 x509_crt_init( &crt );
00969 memset( buf, 0, 2000 );
00970
00971 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
00972 if( strcmp( entity, "subject" ) == 0 )
00973 res = x509_dn_gets( buf, 2000, &crt.subject );
00974 else if( strcmp( entity, "issuer" ) == 0 )
00975 res = x509_dn_gets( buf, 2000, &crt.issuer );
00976 else
00977 TEST_ASSERT( "Unknown entity" == 0 );
00978
00979 x509_crt_free( &crt );
00980
00981 TEST_ASSERT( res != -1 );
00982 TEST_ASSERT( res != -2 );
00983
00984 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
00985 }
00986 #endif
00987 #endif
00988
00989 #ifdef POLARSSL_FS_IO
00990 #ifdef POLARSSL_X509_USE_C
00991 void test_suite_x509_time_expired( char *crt_file, char *entity, int result )
00992 {
00993 x509_crt crt;
00994
00995 x509_crt_init( &crt );
00996
00997 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
00998
00999 if( strcmp( entity, "valid_from" ) == 0 )
01000 TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
01001 else if( strcmp( entity, "valid_to" ) == 0 )
01002 TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
01003 else
01004 TEST_ASSERT( "Unknown entity" == 0 );
01005
01006 x509_crt_free( &crt );
01007 }
01008 #endif
01009 #endif
01010
01011 #ifdef POLARSSL_X509_CRT_PARSE_C
01012 void test_suite_x509parse_crt( char *crt_data, char *result_str, int result )
01013 {
01014 x509_crt crt;
01015 unsigned char buf[2000];
01016 unsigned char output[2000];
01017 int data_len, res;
01018
01019 x509_crt_init( &crt );
01020 memset( buf, 0, 2000 );
01021 memset( output, 0, 2000 );
01022
01023 data_len = unhexify( buf, crt_data );
01024
01025 TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
01026 if( ( result ) == 0 )
01027 {
01028 res = x509_crt_info( (char *) output, 2000, "", &crt );
01029
01030 TEST_ASSERT( res != -1 );
01031 TEST_ASSERT( res != -2 );
01032
01033 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
01034 }
01035
01036 x509_crt_free( &crt );
01037 }
01038 #endif
01039
01040 #ifdef POLARSSL_X509_CRL_PARSE_C
01041 void test_suite_x509parse_crl( char *crl_data, char *result_str, int result )
01042 {
01043 x509_crl crl;
01044 unsigned char buf[2000];
01045 unsigned char output[2000];
01046 int data_len, res;
01047
01048 x509_crl_init( &crl );
01049 memset( buf, 0, 2000 );
01050 memset( output, 0, 2000 );
01051
01052 data_len = unhexify( buf, crl_data );
01053
01054 TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
01055 if( ( result ) == 0 )
01056 {
01057 res = x509_crl_info( (char *) output, 2000, "", &crl );
01058
01059 TEST_ASSERT( res != -1 );
01060 TEST_ASSERT( res != -2 );
01061
01062 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
01063 }
01064
01065 x509_crl_free( &crl );
01066 }
01067 #endif
01068
01069 #ifdef POLARSSL_X509_CRT_PARSE_C
01070 #ifdef POLARSSL_SELF_TEST
01071 void test_suite_x509_selftest()
01072 {
01073 TEST_ASSERT( x509_self_test( 0 ) == 0 );
01074 }
01075 #endif
01076 #endif
01077
01078
01079 #endif
01080
01081
01082 int dep_check( char *str )
01083 {
01084 if( str == NULL )
01085 return( 1 );
01086
01087 if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
01088 {
01089 #if defined(POLARSSL_MD4_C)
01090 return( 0 );
01091 #else
01092 return( 1 );
01093 #endif
01094 }
01095 if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
01096 {
01097 #if defined(POLARSSL_SHA1_C)
01098 return( 0 );
01099 #else
01100 return( 1 );
01101 #endif
01102 }
01103 if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
01104 {
01105 #if defined(POLARSSL_PKCS1_V15)
01106 return( 0 );
01107 #else
01108 return( 1 );
01109 #endif
01110 }
01111 if( strcmp( str, "POLARSSL_PEM_PARSE_C" ) == 0 )
01112 {
01113 #if defined(POLARSSL_PEM_PARSE_C)
01114 return( 0 );
01115 #else
01116 return( 1 );
01117 #endif
01118 }
01119 if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
01120 {
01121 #if defined(POLARSSL_MD5_C)
01122 return( 0 );
01123 #else
01124 return( 1 );
01125 #endif
01126 }
01127 if( strcmp( str, "POLARSSL_ECDSA_C" ) == 0 )
01128 {
01129 #if defined(POLARSSL_ECDSA_C)
01130 return( 0 );
01131 #else
01132 return( 1 );
01133 #endif
01134 }
01135 if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
01136 {
01137 #if defined(POLARSSL_SHA256_C)
01138 return( 0 );
01139 #else
01140 return( 1 );
01141 #endif
01142 }
01143 if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
01144 {
01145 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
01146 return( 0 );
01147 #else
01148 return( 1 );
01149 #endif
01150 }
01151 if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
01152 {
01153 #if defined(POLARSSL_ECP_C)
01154 return( 0 );
01155 #else
01156 return( 1 );
01157 #endif
01158 }
01159 if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
01160 {
01161 #if defined(POLARSSL_RSA_C)
01162 return( 0 );
01163 #else
01164 return( 1 );
01165 #endif
01166 }
01167 if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
01168 {
01169 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
01170 return( 0 );
01171 #else
01172 return( 1 );
01173 #endif
01174 }
01175 if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
01176 {
01177 #if defined(POLARSSL_SHA512_C)
01178 return( 0 );
01179 #else
01180 return( 1 );
01181 #endif
01182 }
01183 if( strcmp( str, "POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3" ) == 0 )
01184 {
01185 #if defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
01186 return( 0 );
01187 #else
01188 return( 1 );
01189 #endif
01190 }
01191
01192
01193 return( 1 );
01194 }
01195
01196 int dispatch_test(int cnt, char *params[50])
01197 {
01198 int ret;
01199 ((void) cnt);
01200 ((void) params);
01201
01202 #if defined(TEST_SUITE_ACTIVE)
01203 if( strcmp( params[0], "x509_cert_info" ) == 0 )
01204 {
01205 #ifdef POLARSSL_FS_IO
01206 #ifdef POLARSSL_X509_CRT_PARSE_C
01207
01208 char *param1 = params[1];
01209 char *param2 = params[2];
01210
01211 if( cnt != 3 )
01212 {
01213 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
01214 return( 2 );
01215 }
01216
01217 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01218 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01219
01220 test_suite_x509_cert_info( param1, param2 );
01221 return ( 0 );
01222 #endif
01223 #endif
01224
01225 return ( 3 );
01226 }
01227 else
01228 if( strcmp( params[0], "x509_crl_info" ) == 0 )
01229 {
01230 #ifdef POLARSSL_FS_IO
01231 #ifdef POLARSSL_X509_CRL_PARSE_C
01232
01233 char *param1 = params[1];
01234 char *param2 = params[2];
01235
01236 if( cnt != 3 )
01237 {
01238 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
01239 return( 2 );
01240 }
01241
01242 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01243 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01244
01245 test_suite_x509_crl_info( param1, param2 );
01246 return ( 0 );
01247 #endif
01248 #endif
01249
01250 return ( 3 );
01251 }
01252 else
01253 if( strcmp( params[0], "x509_verify" ) == 0 )
01254 {
01255 #ifdef POLARSSL_FS_IO
01256 #ifdef POLARSSL_X509_CRT_PARSE_C
01257 #ifdef POLARSSL_X509_CRL_PARSE_C
01258
01259 char *param1 = params[1];
01260 char *param2 = params[2];
01261 char *param3 = params[3];
01262 char *param4 = params[4];
01263 int param5;
01264 int param6;
01265 char *param7 = params[7];
01266
01267 if( cnt != 8 )
01268 {
01269 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
01270 return( 2 );
01271 }
01272
01273 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01274 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01275 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01276 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01277 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
01278 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
01279 if( verify_string( ¶m7 ) != 0 ) return( 2 );
01280
01281 test_suite_x509_verify( param1, param2, param3, param4, param5, param6, param7 );
01282 return ( 0 );
01283 #endif
01284 #endif
01285 #endif
01286
01287 return ( 3 );
01288 }
01289 else
01290 if( strcmp( params[0], "x509_dn_gets" ) == 0 )
01291 {
01292 #ifdef POLARSSL_FS_IO
01293 #ifdef POLARSSL_X509_USE_C
01294
01295 char *param1 = params[1];
01296 char *param2 = params[2];
01297 char *param3 = params[3];
01298
01299 if( cnt != 4 )
01300 {
01301 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01302 return( 2 );
01303 }
01304
01305 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01306 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01307 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01308
01309 test_suite_x509_dn_gets( param1, param2, param3 );
01310 return ( 0 );
01311 #endif
01312 #endif
01313
01314 return ( 3 );
01315 }
01316 else
01317 if( strcmp( params[0], "x509_time_expired" ) == 0 )
01318 {
01319 #ifdef POLARSSL_FS_IO
01320 #ifdef POLARSSL_X509_USE_C
01321
01322 char *param1 = params[1];
01323 char *param2 = params[2];
01324 int param3;
01325
01326 if( cnt != 4 )
01327 {
01328 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01329 return( 2 );
01330 }
01331
01332 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01333 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01334 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01335
01336 test_suite_x509_time_expired( param1, param2, param3 );
01337 return ( 0 );
01338 #endif
01339 #endif
01340
01341 return ( 3 );
01342 }
01343 else
01344 if( strcmp( params[0], "x509parse_crt" ) == 0 )
01345 {
01346 #ifdef POLARSSL_X509_CRT_PARSE_C
01347
01348 char *param1 = params[1];
01349 char *param2 = params[2];
01350 int param3;
01351
01352 if( cnt != 4 )
01353 {
01354 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01355 return( 2 );
01356 }
01357
01358 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01359 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01360 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01361
01362 test_suite_x509parse_crt( param1, param2, param3 );
01363 return ( 0 );
01364 #endif
01365
01366 return ( 3 );
01367 }
01368 else
01369 if( strcmp( params[0], "x509parse_crl" ) == 0 )
01370 {
01371 #ifdef POLARSSL_X509_CRL_PARSE_C
01372
01373 char *param1 = params[1];
01374 char *param2 = params[2];
01375 int param3;
01376
01377 if( cnt != 4 )
01378 {
01379 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01380 return( 2 );
01381 }
01382
01383 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01384 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01385 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01386
01387 test_suite_x509parse_crl( param1, param2, param3 );
01388 return ( 0 );
01389 #endif
01390
01391 return ( 3 );
01392 }
01393 else
01394 if( strcmp( params[0], "x509_selftest" ) == 0 )
01395 {
01396 #ifdef POLARSSL_X509_CRT_PARSE_C
01397 #ifdef POLARSSL_SELF_TEST
01398
01399
01400 if( cnt != 1 )
01401 {
01402 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
01403 return( 2 );
01404 }
01405
01406
01407 test_suite_x509_selftest( );
01408 return ( 0 );
01409 #endif
01410 #endif
01411
01412 return ( 3 );
01413 }
01414 else
01415
01416 {
01417 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
01418 fflush( stdout );
01419 return( 1 );
01420 }
01421 #else
01422 return( 3 );
01423 #endif
01424 return( ret );
01425 }
01426
01427 int get_line( FILE *f, char *buf, size_t len )
01428 {
01429 char *ret;
01430
01431 ret = fgets( buf, len, f );
01432 if( ret == NULL )
01433 return( -1 );
01434
01435 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
01436 buf[strlen(buf) - 1] = '\0';
01437 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
01438 buf[strlen(buf) - 1] = '\0';
01439
01440 return( 0 );
01441 }
01442
01443 int parse_arguments( char *buf, size_t len, char *params[50] )
01444 {
01445 int cnt = 0, i;
01446 char *cur = buf;
01447 char *p = buf, *q;
01448
01449 params[cnt++] = cur;
01450
01451 while( *p != '\0' && p < buf + len )
01452 {
01453 if( *p == '\\' )
01454 {
01455 *p++;
01456 *p++;
01457 continue;
01458 }
01459 if( *p == ':' )
01460 {
01461 if( p + 1 < buf + len )
01462 {
01463 cur = p + 1;
01464 params[cnt++] = cur;
01465 }
01466 *p = '\0';
01467 }
01468
01469 *p++;
01470 }
01471
01472
01473 for( i = 0; i < cnt; i++ )
01474 {
01475 p = params[i];
01476 q = params[i];
01477
01478 while( *p != '\0' )
01479 {
01480 if( *p == '\\' && *(p + 1) == 'n' )
01481 {
01482 p += 2;
01483 *(q++) = '\n';
01484 }
01485 else if( *p == '\\' && *(p + 1) == ':' )
01486 {
01487 p += 2;
01488 *(q++) = ':';
01489 }
01490 else if( *p == '\\' && *(p + 1) == '?' )
01491 {
01492 p += 2;
01493 *(q++) = '?';
01494 }
01495 else
01496 *(q++) = *(p++);
01497 }
01498 *q = '\0';
01499 }
01500
01501 return( cnt );
01502 }
01503
01504 int main()
01505 {
01506 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
01507 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_x509parse.data";
01508 FILE *file;
01509 char buf[5000];
01510 char *params[50];
01511
01512 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01513 unsigned char alloc_buf[1000000];
01514 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
01515 #endif
01516
01517 file = fopen( filename, "r" );
01518 if( file == NULL )
01519 {
01520 fprintf( stderr, "Failed to open\n" );
01521 return( 1 );
01522 }
01523
01524 while( !feof( file ) )
01525 {
01526 int skip = 0;
01527
01528 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01529 break;
01530 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
01531 fprintf( stdout, " " );
01532 for( i = strlen( buf ) + 1; i < 67; i++ )
01533 fprintf( stdout, "." );
01534 fprintf( stdout, " " );
01535 fflush( stdout );
01536
01537 total_tests++;
01538
01539 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01540 break;
01541 cnt = parse_arguments( buf, strlen(buf), params );
01542
01543 if( strcmp( params[0], "depends_on" ) == 0 )
01544 {
01545 for( i = 1; i < cnt; i++ )
01546 if( dep_check( params[i] ) != 0 )
01547 skip = 1;
01548
01549 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01550 break;
01551 cnt = parse_arguments( buf, strlen(buf), params );
01552 }
01553
01554 if( skip == 0 )
01555 {
01556 test_errors = 0;
01557 ret = dispatch_test( cnt, params );
01558 }
01559
01560 if( skip == 1 || ret == 3 )
01561 {
01562 total_skipped++;
01563 fprintf( stdout, "----\n" );
01564 fflush( stdout );
01565 }
01566 else if( ret == 0 && test_errors == 0 )
01567 {
01568 fprintf( stdout, "PASS\n" );
01569 fflush( stdout );
01570 }
01571 else if( ret == 2 )
01572 {
01573 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01574 fclose(file);
01575 exit( 2 );
01576 }
01577 else
01578 total_errors++;
01579
01580 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01581 break;
01582 if( strlen(buf) != 0 )
01583 {
01584 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01585 return( 1 );
01586 }
01587 }
01588 fclose(file);
01589
01590 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01591 if( total_errors == 0 )
01592 fprintf( stdout, "PASSED" );
01593 else
01594 fprintf( stdout, "FAILED" );
01595
01596 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01597 total_tests - total_errors, total_tests, total_skipped );
01598
01599 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01600 #if defined(POLARSSL_MEMORY_DEBUG)
01601 memory_buffer_alloc_status();
01602 #endif
01603 memory_buffer_alloc_free();
01604 #endif
01605
01606 return( total_errors != 0 );
01607 }
01608
01609