📄 testlib.c
字号:
#endif /* 0 */
const char *driverPath = "c:/program files/eracom/cprov sw/cryptoki.dll"; /* Eracom (old, OK) */
printf( "Updating cryptlib configuration to load PKCS #11 driver\n "
"'%s'\n as default driver...", driverPath );
/* Set the path for a PKCS #11 device driver. We only enable one of
these at a time to speed the startup time */
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_DEVICE_PKCS11_DVR01,
driverPath, strlen( driverPath ) );
/* Update the options */
cryptSetAttribute( CRYPT_UNUSED, CRYPT_OPTION_CONFIGCHANGED, FALSE );
puts( " done." );
}
/* Add trusted certs to the config file and make sure that they're
persistent. This can't be done in the normal self-test since it requires
that cryptlib be restarted as part of the test to re-read the config file,
and because it modifies the cryptlib config file */
static void updateConfigCert( void )
{
CRYPT_CERTIFICATE trustedCert;
int status;
/* Import the first cert, make it trusted, and commit the changes */
importCertFromTemplate( &trustedCert, CERT_FILE_TEMPLATE, 1 );
cryptSetAttribute( trustedCert, CRYPT_CERTINFO_TRUSTED_IMPLICIT, TRUE );
cryptSetAttribute( CRYPT_UNUSED, CRYPT_OPTION_CONFIGCHANGED, FALSE );
cryptDestroyCert( trustedCert );
cryptEnd();
/* Do the same with a second cert. At the conclusion of this, we should
have two trusted certs on disk */
status = cryptInit();
if( cryptStatusError( status ) )
{
puts( "Couldn't reload cryptlib configuration." );
return;
}
importCertFromTemplate( &trustedCert, CERT_FILE_TEMPLATE, 2 );
cryptSetAttribute( trustedCert, CRYPT_CERTINFO_TRUSTED_IMPLICIT, TRUE );
cryptSetAttribute( CRYPT_UNUSED, CRYPT_OPTION_CONFIGCHANGED, FALSE );
cryptDestroyCert( trustedCert );
cryptEnd();
}
/****************************************************************************
* *
* Misc.Kludges *
* *
****************************************************************************/
/* Generic test code insertion point. The following routine is called
before any of the other tests are run and can be used to handle special-
case tests that aren't part of the main test suite */
void testKludge( void )
{
#if 0
/* pscp/psftp client requests a subsystem but cryptlib server doesn't
report the subsystem request */
// testSessionSSH_SFTPServer();
// testSessionSSHServer();
// checkCreateDatabaseKeysets();
// testSessionSCEPCACertClientServer();
/* Since this is a special-case test we don't want to fall through to
the main test code so we exit here */
cryptEnd();
puts( "\nPress a key to exit." );
getchar();
exit( EXIT_SUCCESS );
#endif /* 0 */
/* Performance-testing test harness */
#if 0
void performanceTests( const CRYPT_DEVICE cryptDevice );
performanceTests( CRYPT_UNUSED );
#endif /* 0 */
/* Memory diagnostic test harness */
#if 0
testReadFileCertPrivkey();
testEnvelopePKCCrypt(); /* Use "Datasize, certificate" */
testEnvelopeSign(); /* Use "Datasize, certificate" */
#endif /* 0 */
/* Simple (brute-force) server code. NB: Remember to change
setLocalConnect() to not bind the server to localhost if expecting
external connections */
#if 0
while( TRUE )
testSessionTSPServer();
#endif /* 0 */
}
/****************************************************************************
* *
* Main Test Code *
* *
****************************************************************************/
/* 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;
BOOLEAN algosEnabled;
#endif /* TEST_LOWLEVEL */
#ifdef TEST_CONFIG
int i;
#endif /* TEST_CONFIG */
#if defined( TEST_SELFTEST ) || defined( TEST_CONFIG )
int value;
#endif /* TEST_SELFTEST || TEST_CONFIG */
int status;
void testSystemSpecific1( void );
void testSystemSpecific2( void );
/* Get rid of compiler warnings */
if( argc || argv );
#ifdef USE_TCHECK
THREAD_DEBUG_SUSPEND();
#endif /* USE_TCHECK */
/* Make sure that various system-specific features are set right */
testSystemSpecific1();
/* 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, line %d.\n", status,
__LINE__ );
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 */
#if defined( __MVS__ ) || defined( __VMCMS__ )
#pragma convlit( resume )
cryptAddRandom( "xyzzy", 5 );
#pragma convlit( suspend )
#else
cryptAddRandom( "xyzzy", 5 );
#endif /* Special-case EBCDIC handling */
#endif /* TEST_RANDOM */
/* Perform a general sanity check to make sure that the self-test is
being run the right way */
if( !checkFileAccess() )
goto errorExit;
/* Make sure that further system-specific features that require cryptlib
to be initialised to check are set right */
#ifndef _WIN32_WCE
testSystemSpecific2();
#endif /* WinCE */
#ifdef USE_TCHECK
THREAD_DEBUG_RESUME();
#endif /* USE_TCHECK */
/* 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,
TRUE );
if( cryptStatusError( status ) )
{
printf( "Attempt to perform cryptlib algorithm self-test failed "
"with error code %d, line %d.\n", status, __LINE__ );
goto errorExit;
}
status = cryptGetAttribute( CRYPT_UNUSED, CRYPT_OPTION_SELFTESTOK,
&value );
if( cryptStatusError( status ) || value != TRUE )
{
/* Unfortunately all that we can report at this point is that the
self-test failed, we can't try each algorithm individually
because the self-test has disabled the failed one(s) */
printf( "cryptlib algorithm self-test failed, line %d.\n",
__LINE__ );
goto errorExit;
}
puts( "cryptlib algorithm self-test succeeded.\n" );
#endif /* TEST_SELFTEST */
#ifdef TEST_LOWLEVEL
/* Test the conventional encryption routines */
algosEnabled = FALSE;
for( cryptAlgo = CRYPT_ALGO_FIRST_CONVENTIONAL;
cryptAlgo <= CRYPT_ALGO_LAST_CONVENTIONAL; cryptAlgo++ )
{
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) )
{
if( !testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
algosEnabled = TRUE;
}
}
if( !algosEnabled )
puts( "(No conventional-encryption algorithms enabled)." );
/* Test the public-key encryption routines */
algosEnabled = FALSE;
for( cryptAlgo = CRYPT_ALGO_FIRST_PKC;
cryptAlgo <= CRYPT_ALGO_LAST_PKC; cryptAlgo++ )
{
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) )
{
if( !testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
algosEnabled = TRUE;
}
}
if( cryptStatusOK( cryptQueryCapability( CRYPT_ALGO_RSA, NULL ) ) && \
!testRSAMinimalKey() )
goto errorExit;
if( !algosEnabled )
puts( "(No public-key algorithms enabled)." );
/* Test the hash routines */
algosEnabled = FALSE;
for( cryptAlgo = CRYPT_ALGO_FIRST_HASH;
cryptAlgo <= CRYPT_ALGO_LAST_HASH; cryptAlgo++ )
{
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) )
{
if( !testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
algosEnabled = TRUE;
}
}
if( !algosEnabled )
puts( "(No hash algorithms enabled)." );
/* Test the MAC routines */
algosEnabled = FALSE;
for( cryptAlgo = CRYPT_ALGO_FIRST_MAC;
cryptAlgo <= CRYPT_ALGO_LAST_MAC; cryptAlgo++ )
{
if( cryptStatusOK( cryptQueryCapability( cryptAlgo, NULL ) ) )
{
if( !testLowlevel( CRYPT_UNUSED, cryptAlgo, FALSE ) )
goto errorExit;
algosEnabled = TRUE;
}
}
if( !algosEnabled )
puts( "(No MAC algorithms enabled)." );
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 )
{
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 );
assert( length < 256 );
#ifdef UNICODE_STRINGS
buffer[ length / sizeof( wchar_t ) ] = TEXT( '\0' );
printf( "%s = %S.\n", configOption[ i ].name, buffer );
#else
buffer[ length ] = '\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( cryptStatusOK( cryptQueryCapability( CRYPT_ALGO_HMAC_SHA1, NULL ) ) )
{
/* Only test the MAC functions of HMAC-SHA1 is enabled */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -