📄 testscrt.c
字号:
caInfo[ 10 ].url,
strlen( caInfo[ 10 ].url ) );
if( cryptStatusOK( status ) )
status = cryptSetAttribute( cryptSession,
CRYPT_SESSINFO_CMP_PRIVKEYSET,
cryptKeyset );
cryptKeysetClose( cryptKeyset );
if( cryptStatusError( status ) )
{
printf( "Addition of session information failed with error code %d, "
"line %d.\n", status, __LINE__ );
return( FALSE );
}
/* Activate the session */
status = cryptSetAttribute( cryptSession, CRYPT_SESSINFO_ACTIVE, TRUE );
if( cryptStatusError( status ) )
{
printExtError( cryptSession, "Attempt to activate plug-and-play PKI "
"client session", status, __LINE__ );
cryptDestroySession( cryptSession );
return( FALSE );
}
/* Clean up */
cryptDestroySession( cryptSession );
return( TRUE );
}
int testSessionPNPPKI( void )
{
return( connectPNPPKI() );
}
/* Test the CMP server */
static int cmpServerSingleIteration( const CRYPT_CONTEXT cryptPrivateKey,
const CRYPT_KEYSET cryptCertStore )
{
CRYPT_SESSION cryptSession;
int status;
/* Create the CMP session and add the CA key and cert store */
status = cryptCreateSession( &cryptSession, CRYPT_UNUSED,
CRYPT_SESSION_CMP_SERVER );
if( cryptStatusError( status ) )
{
printf( "SVR: cryptCreateSession() failed with error code %d, line "
"%d.\n", status, __LINE__ );
return( FALSE );
}
status = cryptSetAttribute( cryptSession,
CRYPT_SESSINFO_PRIVATEKEY, cryptPrivateKey );
if( cryptStatusOK( status ) )
status = cryptSetAttribute( cryptSession,
CRYPT_SESSINFO_KEYSET, cryptCertStore );
if( cryptStatusError( status ) )
return( attrErrorExit( cryptSession, "SVR: cryptSetAttribute()",
status, __LINE__ ) );
if( !setLocalConnect( cryptSession, 80 ) )
return( FALSE );
/* Activate the session */
status = activatePersistentServerSession( cryptSession, TRUE );
if( cryptStatusError( status ) )
return( extErrorExit( cryptSession, "SVR: Attempt to activate CMP "
"server session", status, __LINE__ ) );
/* We processed the request, clean up */
cryptDestroySession( cryptSession );
return( TRUE );
}
static int cmpServerInit( CRYPT_CONTEXT *cryptPrivateKey,
CRYPT_KEYSET *cryptCertStore )
{
int status;
/* Get the cert store and server private key to use with the session.
Before we add the store we perform a cleanup action to remove any
leftover requests from previous runs */
status = cryptKeysetOpen( cryptCertStore, CRYPT_UNUSED,
CERTSTORE_KEYSET_TYPE, CERTSTORE_KEYSET_NAME,
CRYPT_KEYOPT_CREATE );
if( status == CRYPT_ERROR_PARAM3 )
{
/* This type of keyset access isn't available, return a special error
code to indicate that the test wasn't performed, but that this
isn't a reason to abort processing */
puts( "SVR: No certificate store available, aborting CMP server "
"test.\n" );
return( CRYPT_ERROR_NOTAVAIL );
}
if( status == CRYPT_ERROR_DUPLICATE )
status = cryptKeysetOpen( cryptCertStore, CRYPT_UNUSED,
CERTSTORE_KEYSET_TYPE, CERTSTORE_KEYSET_NAME,
CRYPT_KEYOPT_NONE );
if( cryptStatusError( status ) )
{
printf( "SVR: cryptKeysetOpen() failed with error code %d, line "
"%d.\n", status, __LINE__ );
return( FALSE );
}
cryptCACertManagement( NULL, CRYPT_CERTACTION_CLEANUP, *cryptCertStore,
CRYPT_UNUSED, CRYPT_UNUSED );
status = getPrivateKey( cryptPrivateKey, CA_PRIVKEY_FILE,
CA_PRIVKEY_LABEL, TEST_PRIVKEY_PASSWORD );
if( cryptStatusError( status ) )
{
printf( "SVR: CA private key read failed with error code %d, "
"line %d.\n", status, __LINE__ );
return( FALSE );
}
return( TRUE );
}
int testSessionCMPServer( void )
{
CRYPT_SESSION cryptSession;
CRYPT_CONTEXT cryptCAKey;
CRYPT_KEYSET cryptCertStore;
int caCertTrusted, i, status;
puts( "SVR: Testing CMP server session..." );
/* Perform a test create of a CMP server session to verify that we can
do this test */
status = cryptCreateSession( &cryptSession, CRYPT_UNUSED,
CRYPT_SESSION_CMP_SERVER );
if( status == CRYPT_ERROR_PARAM3 ) /* CMP session access not available */
return( CRYPT_ERROR_NOTAVAIL );
if( cryptStatusError( status ) )
{
printf( "SVR: cryptCreateSession() failed with error code %d, "
"line %d.\n", status, __LINE__ );
return( FALSE );
}
cryptDestroySession( cryptSession );
/* Get the information needed by the server */
if( !cmpServerInit( &cryptCAKey, &cryptCertStore ) )
return( FALSE );
/* Make the CA key trusted for PKIBoot functionality */
cryptGetAttribute( cryptCAKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT,
&caCertTrusted );
cryptSetAttribute( cryptCAKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT, 1 );
/* Run the server several times to handle the different requests */
for( i = 0; i < NO_CA_REQUESTS; i++ )
{
printf( "SVR: Running server iteration %d.\n", i + 1 );
if( !cmpServerSingleIteration( cryptCAKey, cryptCertStore ) )
break;
}
if( i == 0 )
/* None of the requests succeeded */
return( FALSE );
printf( "SVR: %d of %d server requests were processed.\n", i,
NO_CA_REQUESTS );
/* Issue a CRL to make sure that the revocation was performed correctly.
We do this now because the cert management self-test can't easily
perform the check because it requires a CMP-revoked cert in order to
function */
if( i == NO_CA_REQUESTS )
{
CRYPT_CERTIFICATE cryptCRL;
int noEntries = 0;
/* Issue the CRL */
status = cryptCACertManagement( &cryptCRL, CRYPT_CERTACTION_ISSUE_CRL,
cryptCertStore, cryptCAKey,
CRYPT_UNUSED );
if( cryptStatusError( status ) )
return( extErrorExit( cryptCertStore, "cryptCACertManagement()",
status, __LINE__ ) );
/* Make sure that the CRL contains at least one entry */
if( cryptStatusOK( cryptSetAttribute( cryptCRL,
CRYPT_CERTINFO_CURRENT_CERTIFICATE,
CRYPT_CURSOR_FIRST ) ) )
do
noEntries++;
while( cryptSetAttribute( cryptCRL,
CRYPT_CERTINFO_CURRENT_CERTIFICATE,
CRYPT_CURSOR_NEXT ) == CRYPT_OK );
if( noEntries <= 0 )
{
puts( "CRL created from revoked certificate is empty, should "
"contain at least one\ncertificate entry." );
return( FALSE );
}
/* Clean up */
cryptDestroyCert( cryptCRL );
}
/* Clean up */
if( !caCertTrusted )
cryptSetAttribute( cryptCAKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT, 0 );
cryptKeysetClose( cryptCertStore );
cryptDestroyContext( cryptCAKey );
puts( "SVR: CMP session succeeded.\n" );
return( TRUE );
}
/* Perform a client/server loopback test */
#ifdef WINDOWS_THREADS
static int pnppkiServer( const BOOLEAN isPkiBoot )
{
CRYPT_CONTEXT cryptPrivateKey;
CRYPT_KEYSET cryptCertStore;
int caCertTrusted;
printf( "SVR: Testing %s server session...\n",
isPkiBoot ? "PKIBoot" : "plug-and-play PKI" );
/* Get the information needed by the server */
if( !cmpServerInit( &cryptPrivateKey, &cryptCertStore ) )
return( FALSE );
/* Make the CA key trusted for PKIBoot functionality */
cryptGetAttribute( cryptPrivateKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT,
&caCertTrusted );
cryptSetAttribute( cryptPrivateKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT, 1 );
/* Run the server once to handle the plug-and-play PKI process */
if( !cmpServerSingleIteration( cryptPrivateKey, cryptCertStore ) )
return( FALSE );
/* Clean up */
if( !caCertTrusted )
cryptSetAttribute( cryptPrivateKey,
CRYPT_CERTINFO_TRUSTED_IMPLICIT, 0 );
cryptKeysetClose( cryptCertStore );
cryptDestroyContext( cryptPrivateKey );
puts( "SVR: Plug-and-play PKI session succeeded.\n" );
return( TRUE );
}
unsigned __stdcall cmpServerThread( void *dummy )
{
testSessionCMPServer();
_endthreadex( 0 );
return( 0 );
}
int testSessionCMPClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
#if ( CA_NO != 1 ) && ( CA_NO != 10 )
/* Because the code has to handle so many CA-specific peculiarities, we
can only perform this test when the CA being used is the cryptlib
CA */
puts( "Error: The local CMP session test only works with CA_NO == 1 "
"or 10." );
return( FALSE );
#endif /* cryptlib CA */
/* Start the server and wait for it to initialise (this takes a bit
longer than the other servers because we have to work with a cert
store so we wait a bit longer than usual) */
hThread = ( HANDLE ) _beginthreadex( NULL, 0, &cmpServerThread,
NULL, 0, &threadID );
Sleep( 3000 );
/* Connect to the local server */
status = connectCMP( FALSE );
if( WaitForSingleObject( hThread, 15000 ) == WAIT_TIMEOUT )
{
puts( "Warning: Server thread is still active due to session "
"negotiation failure,\n this will cause an error "
"condition when cryptEnd() is called due\n to "
"resources remaining allocated. Press a key to continue." );
getchar();
}
CloseHandle( hThread );
return( status );
}
unsigned __stdcall cmpPKIBootServerThread( void *dummy )
{
pnppkiServer( TRUE );
_endthreadex( 0 );
return( 0 );
}
int testSessionCMPPKIBootClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
#if ( CA_NO != 1 ) && ( CA_NO != 10 )
/* Because the code has to handle so many CA-specific peculiarities, we
can only perform this test when the CA being used is the cryptlib
CA */
puts( "Error: The local CMP session test only works with CA_NO == 1 "
"or 10." );
return( FALSE );
#endif /* cryptlib CA */
/* Start the server and wait for it to initialise (this takes a bit
longer than the other servers because we have to work with a cert
store so we wait a bit longer than usual) */
hThread = ( HANDLE ) _beginthreadex( NULL, 0, &cmpPKIBootServerThread,
NULL, 0, &threadID );
Sleep( 3000 );
/* Connect to the local server with PKIBoot enabled */
status = connectCMP( TRUE );
if( WaitForSingleObject( hThread, 15000 ) == WAIT_TIMEOUT )
{
puts( "Warning: Server thread is still active due to session "
"negotiation failure,\n this will cause an error "
"condition when cryptEnd() is called due\n to "
"resources remaining allocated. Press a key to continue." );
getchar();
}
CloseHandle( hThread );
return( status );
}
unsigned __stdcall cmpPnPPKIServerThread( void *dummy )
{
pnppkiServer( FALSE );
_endthreadex( 0 );
return( 0 );
}
int testSessionPNPPKIClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
/* Start the server and wait for it to initialise (this takes a bit
longer than the other servers because we have to work with a cert
store so we wait a bit longer than usual) */
hThread = ( HANDLE ) _beginthreadex( NULL, 0, &cmpPnPPKIServerThread,
NULL, 0, &threadID );
Sleep( 3000 );
/* Connect to the local server with PKIBoot enabled */
status = connectPNPPKI();
if( WaitForSingleObject( hThread, 15000 ) == WAIT_TIMEOUT )
{
puts( "Warning: Server thread is still active due to session "
"negotiation failure,\n this will cause an error "
"condition when cryptEnd() is called due\n to "
"resources remaining allocated. Press a key to continue." );
getchar();
}
CloseHandle( hThread );
return( status );
}
#endif /* WINDOWS_THREADS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -