📄 testlib.c
字号:
external connections */
#if 0
while( TRUE )
testSessionTSPServer();
#endif /* 0 */
/* Functions that can be pressed into service in combination with the
special-purpose defines at the start of testkeyf.c to generate custom
certs/keys for the self-test */
#if 0
testWriteFileCertChain(); /* To generate user/intermed.CA priv.key+cert */
#endif /* 0 */
#if 0
testReadWriteFileKey();
testUpdateFileCert(); /* To generate CA priv.key+cert */
#endif /* 0 */
#if 0
puts( "Hit a key..." );
getchar();
if( cryptEnd() == CRYPT_ERROR_INCOMPLETE )
{
puts( "Objects remained allocated." );
getchar();
}
exit( 0 );
#endif /* 0 */
}
/****************************************************************************
* *
* Main Test Code *
* *
****************************************************************************/
#ifdef __WINDOWS__
#define INC_CHILD
#endif /* __WINDOWS__ */
/* Comprehensive cryptlib stress test. To get the following to run under
WinCE as a native console app, it's necessary to change the entry point
in Settings | Link | Output from WinMainCRTStartup to the undocumented
mainACRTStartup, which calls main() rather than WinMain(), however this
only works if the system has a native console-mode driver (most don't) */
int main( int argc, char **argv )
{
#ifdef TEST_LOWLEVEL
CRYPT_ALGO_TYPE cryptAlgo;
#endif /* TEST_LOWLEVEL */
#ifdef TEST_CONFIG
int i;
#endif /* TEST_CONFIG */
#ifdef TEST_SELFTEST
int value;
#endif /* TEST_SELFTEST */
int status;
void testSystemSpecific( void );
/* Get rid of compiler warnings */
if( argc || argv );
/* Make sure various system-specific features are set right */
testSystemSpecific();
/* VisualAge C++ doesn't set the TZ correctly. The check for this isn't
as simple as it would seem since most IBM compilers define the same
preprocessor values even though it's not documented anywhere, so we
have to enable the tzset() call for (effectively) all IBM compilers
and then disable it for ones other than VisualAge C++ */
#if ( defined( __IBMC__ ) || defined( __IBMCPP__ ) ) && !defined( __VMCMS__ )
tzset();
#endif /* VisualAge C++ */
/* Initialise cryptlib */
status = cryptInit();
if( cryptStatusError( status ) )
{
printf( "cryptInit() failed with error code %d.\n", status );
exit( EXIT_FAILURE );
}
#ifndef TEST_RANDOM
/* In order to avoid having to do a randomness poll for every test run,
we bypass the randomness-handling by adding some junk. This is only
enabled when cryptlib is built in debug mode, so it won't work with
any production systems */
cryptAddRandom( "xyzzy", 5 );
#endif /* TEST_RANDOM */
/* Perform a general sanity check to make sure that the self-test is
being run the right way */
if( !checkFileAccess() )
exit( EXIT_FAILURE );
/* For general testing purposes we can insert test code at this point to
test special cases that aren't covered in the general tests below */
testKludge();
#ifdef SMOKE_TEST
/* Perform a general smoke test of the kernel */
smokeTest();
#endif /* SMOKE_TEST */
#ifdef TEST_SELFTEST
/* Perform the self-test. First we write the value to true to force a
self-test, then we read it back to see whether it succeeded */
status = cryptSetAttribute( CRYPT_UNUSED, CRYPT_OPTION_SELFTESTOK,
CRYPT_USE_DEFAULT );
if( cryptStatusError( status ) )
{
printf( "Attempt to perform cryptlib algorithm self-test failed "
"with error code %d.\n", status );
exit( EXIT_FAILURE );
}
status = cryptGetAttribute( CRYPT_UNUSED, CRYPT_OPTION_SELFTESTOK, &value );
if( cryptStatusError( status ) || value != CRYPT_USE_DEFAULT )
{
puts( "cryptlib algorithm self-test failed." );
exit( EXIT_FAILURE );
}
puts( "cryptlib algorithm self-test succeeded.\n" );
#endif /* TEST_SELFTEST */
#ifdef TEST_LOWLEVEL
/* Test the conventional encryption routines */
for( cryptAlgo = CRYPT_ALGO_FIRST_CONVENTIONAL;
cryptAlgo <= CRYPT_ALGO_LAST_CONVENTIONAL; cryptAlgo++ )
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) && \
!testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
/* Test the public-key encryption routines */
for( cryptAlgo = CRYPT_ALGO_FIRST_PKC;
cryptAlgo <= CRYPT_ALGO_LAST_PKC; cryptAlgo++ )
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) && \
!testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
/* Test the hash routines */
for( cryptAlgo = CRYPT_ALGO_FIRST_HASH;
cryptAlgo <= CRYPT_ALGO_LAST_HASH; cryptAlgo++ )
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) && \
!testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
/* Test the MAC routines */
for( cryptAlgo = CRYPT_ALGO_FIRST_MAC;
cryptAlgo <= CRYPT_ALGO_LAST_MAC; cryptAlgo++ )
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) && \
!testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
printf( "\n" );
#else
puts( "Skipping test of low-level encryption routines...\n" );
#endif /* TEST_LOWLEVEL */
/* Test the randomness-gathering routines */
#ifdef TEST_RANDOM
if( !testRandomRoutines() )
{
puts( "The self-test will proceed without using a strong random "
"number source.\n" );
/* Kludge the randomness routines so we can continue the self-tests */
cryptAddRandom( "xyzzy", 5 );
}
#else
puts( "Skipping test of randomness routines...\n" );
#endif /* TEST_RANDOM */
/* Test the configuration options routines */
#ifdef TEST_CONFIG
for( i = 0; configOption[ i ].option != CRYPT_ATTRIBUTE_NONE; i++ )
{
if( configOption[ i ].isNumeric )
{
int value;
cryptGetAttribute( CRYPT_UNUSED, configOption[ i ].option, &value );
printf( "%s = %d.\n", configOption[ i ].name, value );
}
else
{
C_CHR buffer[ 256 ];
int length;
cryptGetAttributeString( CRYPT_UNUSED, configOption[ i ].option,
buffer, &length );
#ifdef UNICODE_STRINGS
buffer[ length / sizeof( wchar_t ) ] = TEXT( '\0' );
printf( "%s = %S.\n", configOption[ i ].name, buffer );
#else
buffer[ length ] = TEXT( '\0' );
printf( "%s = %s.\n", configOption[ i ].name, buffer );
#endif /* UNICODE_STRINGS */
}
}
printf( "\n" );
#else
puts( "Skipping display of config options...\n" );
#endif /* TEST_CONFIG */
/* Test the crypto device routines */
#ifdef TEST_DEVICE
status = testDevices();
if( status == CRYPT_ERROR_NOTAVAIL )
puts( "Handling for crypto devices doesn't appear to be enabled in "
"this build of\ncryptlib.\n" );
else
if( !status )
goto errorExit;
#else
puts( "Skipping test of crypto device routines...\n" );
#endif /* TEST_DEVICE */
/* Test the mid-level routines */
#ifdef TEST_MIDLEVEL
if( !testLargeBufferEncrypt() )
goto errorExit;
if( !testDeriveKey() )
goto errorExit;
if( !testConventionalExportImport() )
goto errorExit;
if( !testMACExportImport() )
goto errorExit;
if( !testKeyExportImport() )
goto errorExit;
if( !testSignData() )
goto errorExit;
/* Disabled for now since there's no useful DH mechanism defined in any
standard. Note that KEA is still tested via the Fortezza device test
if( !testKeyAgreement() )
goto errorExit; */
if( !testKeygen() )
goto errorExit;
if( !testKeygenAsync() )
goto errorExit;
/* No need for putchar, mid-level functions leave a blank line at end */
#else
puts( "Skipping test of mid-level encryption routines...\n" );
#endif /* TEST_MIDLEVEL */
/* Test the certificate management routines */
#ifdef TEST_CERT
if( !testCert() )
goto errorExit;
if( !testCACert() )
goto errorExit;
if( !testXyzzyCert() )
goto errorExit;
if( !testTextStringCert() )
goto errorExit;
if( !testComplexCert() )
goto errorExit;
if( !testCertExtension() )
goto errorExit;
if( !testCustomDNCert() )
goto errorExit;
if( !testSETCert() )
goto errorExit;
if( !testAttributeCert() )
goto errorExit;
if( !testCertRequest() )
goto errorExit;
if( !testComplexCertRequest() )
goto errorExit;
if( !testCRMFRequest() )
goto errorExit;
if( !testComplexCRMFRequest() )
goto errorExit;
if( !testCRL() )
goto errorExit;
if( !testComplexCRL() )
goto errorExit;
if( !testRevRequest() )
goto errorExit;
if( !testCertChain() )
goto errorExit;
if( !testCMSAttributes() )
goto errorExit;
if( !testOCSPReqResp() )
goto errorExit;
if( !testCertImport() )
goto errorExit;
if( !testCertReqImport() )
goto errorExit;
if( !testCRLImport() )
goto errorExit;
if( !testCertChainImport() )
goto errorExit;
if( !testOCSPImport() )
goto errorExit;
if( !testBase64CertImport() )
goto errorExit;
if( !testBase64CertChainImport() )
goto errorExit;
if( !testMiscImport() )
goto errorExit;
if( !testCertComplianceLevel() )
goto errorExit;
#if 0 /* This takes a while to run and produces a lot of output that won't
be meaningful to anyone other than cryptlib developers so it's
disabled by default */
if( !testPathProcessing() )
goto errorExit;
#endif /* 0 */
#else
puts( "Skipping test of certificate managment routines...\n" );
#endif /* TEST_CERT */
/* Test the keyset read routines */
#ifdef TEST_KEYSET
#ifdef DATABASE_AUTOCONFIG
checkCreateDatabaseKeysets();
#endif /* DATABASE_AUTOCONFIG */
if( !testGetPGPPublicKey() )
goto errorExit;
if( !testGetPGPPrivateKey() )
goto errorExit;
if( !testGetBorkenKey() )
goto errorExit;
if( !testReadWriteFileKey() )
goto errorExit;
if( !testReadBigFileKey() )
goto errorExit;
if( !testReadFilePublicKey() )
goto errorExit;
if( !testDeleteFileKey() )
goto errorExit;
if( !testUpdateFileCert() )
goto errorExit;
if( !testReadFileCert() )
goto errorExit;
if( !testReadFileCertPrivkey() )
goto errorExit;
if( !testWriteFileCertChain() )
goto errorExit;
if( !testReadFileCertChain() )
goto errorExit;
if( !testAddTrustedCert() )
goto errorExit;
#if 0 /* This changes the global config file and is disabled by default */
if( !testAddGloballyTrustedCert() )
goto errorExit;
#endif /* 0 */
if( !testWriteFileLongCertChain() )
goto errorExit;
if( !testSingleStepFileCert() )
goto errorExit;
if( !testDoubleCertFile() )
goto errorExit;
if( !testRenewedCertFile() )
goto errorExit;
status = testWriteCert();
if( status == CRYPT_ERROR_NOTAVAIL )
puts( "Handling for certificate databases doesn't appear to be "
"enabled in this\nbuild of cryptlib, skipping the test of "
"the certificate database routines.\n" );
else
if( status )
{
if( !testReadCert() )
goto errorExit;
if( !testKeysetQuery() )
goto errorExit;
/* The database plugin test will usually fail unless the user has
set up a plugin, so we don't check the return value */
testWriteCertDbx();
}
/* For the following tests we may have read access but not write access,
so we test a read of known-present certs before trying a write -
unlike the local keysets we don't need to add a cert before we can try
reading it */
status = testReadCertLDAP();
if( status == CRYPT_ERROR_NOTAVAIL )
puts( "Handling for LDAP certificate directories doesn't appear to "
"be enabled in\nthis build of cryptlib, skipping the test of "
"the certificate directory\nroutines.\n" );
else
/* LDAP access can fail if the directory doesn't use the standard
du jour, so we don't treat a failure as a fatal error */
if( status )
{
/* LDAP writes are even worse than LDAP reads, so we don't
treat failures here as fatal either */
testWriteCertLDAP();
}
status = testReadCertURL();
if( status == CRYPT_ERROR_NOTAVAIL )
puts( "Handling for fetching certificates from web pages doesn't "
"appear to be\nenabled in this build of cryptlib, skipping "
"the test of the HTTP routines.\n" );
else
/* Being able to read a cert from a web page is rather different from
access to an HTTP cert store, so we don't treat an error here as
fatal */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -