📄 s_cmp.c
字号:
int testSessionPNPPKI( void )
{
return( connectPNPPKI( FALSE, FALSE, FALSE ) );
}
/* Test the CMP server */
static int cmpServerSingleIteration( const CRYPT_CONTEXT cryptPrivateKey,
const CRYPT_KEYSET cryptCertStore,
const BOOLEAN useDevice )
{
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( cryptStatusOK( status ) && useDevice )
{
/* Keygen on a device can take an awfully long time for some devices,
so we set an extended timeout to allow for this */
cryptSetAttribute( cryptSession, CRYPT_OPTION_NET_READTIMEOUT,
NET_TIMEOUT );
status = cryptSetAttribute( cryptSession,
CRYPT_OPTION_NET_WRITETIMEOUT,
NET_TIMEOUT );
}
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 ) )
{
status = extErrorExit( cryptSession, "SVR: Attempt to activate CMP "
"server session", status, __LINE__ );
cryptDestroySession( cryptSession );
return( status );
}
/* We processed the request, clean up */
cryptDestroySession( cryptSession );
return( TRUE );
}
int testSessionCMPServer( void )
{
CRYPT_SESSION cryptSession;
CRYPT_CONTEXT cryptCAKey;
CRYPT_KEYSET cryptCertStore;
int caCertTrusted, i, status;
/* Acquire the init mutex */
acquireMutex();
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 );
/* Set up the server-side objects */
if( !pkiServerInit( &cryptCAKey, &cryptCertStore, CA_PRIVKEY_FILE,
CA_PRIVKEY_LABEL, cmpPkiUserData, cmpPkiUserCaData,
"CMP" ) )
return( FALSE );
/* Make the CA key trusted for PKIBoot functionality */
cryptGetAttribute( cryptCAKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT,
&caCertTrusted );
cryptSetAttribute( cryptCAKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT, 1 );
/* Tell the client that we're ready to go */
releaseMutex();
/* 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, FALSE ) )
{
#ifdef SERVER_IS_CRYPTLIB
/* If we're running the loopback test and this is the second
iteration, the client is testing the ability to detect a
duplicate ir, so a failure is expected */
if( i == 1 )
{
puts( "SVR: Failure was due to a rejected duplicate request "
"from the client,\n continuing..." );
continue;
}
#endif /* SERVER_IS_CRYPTLIB */
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 pkiBootOnly, const BOOLEAN isCaUser,
const BOOLEAN isIntermediateCA,
const BOOLEAN useDevice )
{
CRYPT_CONTEXT cryptCAKey;
CRYPT_KEYSET cryptCertStore;
int caCertTrusted;
/* Acquire the PNP PKI init mutex */
acquireMutex();
printf( "SVR: Testing %s server session%s...\n",
pkiBootOnly ? "PKIBoot" : "plug-and-play PKI",
isCaUser ? " for CA cert" : \
isIntermediateCA ? " using intermediate CA" : "" );
/* Get the information needed by the server */
if( isIntermediateCA )
{
/* The intermediate CA has a PnP-generated, so the key label is
the predefined PnP signature key one */
if( !pkiServerInit( &cryptCAKey, &cryptCertStore,
PNPCA_PRIVKEY_FILE, TEXT( "Signature key" ),
cmpPkiUserData, cmpPkiUserCaData, "CMP" ) )
return( FALSE );
}
else
{
if( !pkiServerInit( &cryptCAKey, &cryptCertStore, CA_PRIVKEY_FILE,
CA_PRIVKEY_LABEL, cmpPkiUserData,
cmpPkiUserCaData, "CMP" ) )
return( FALSE );
}
/* Make the CA key trusted for PKIBoot functionality */
cryptGetAttribute( cryptCAKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT,
&caCertTrusted );
cryptSetAttribute( cryptCAKey, CRYPT_CERTINFO_TRUSTED_IMPLICIT, 1 );
/* Tell the client that we're ready to go */
releaseMutex();
/* Run the server once to handle the plug-and-play PKI process */
if( !cmpServerSingleIteration( cryptCAKey, cryptCertStore, useDevice ) )
return( FALSE );
/* Clean up */
if( !caCertTrusted )
cryptSetAttribute( cryptCAKey,
CRYPT_CERTINFO_TRUSTED_IMPLICIT, 0 );
cryptKeysetClose( cryptCertStore );
cryptDestroyContext( cryptCAKey );
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;
#ifndef SERVER_IS_CRYPTLIB
/* 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 the cryptlib "
"CA." );
return( FALSE );
#endif /* !SERVER_IS_CRYPTLIB */
/* Start the server */
createMutex();
hThread = ( HANDLE ) _beginthreadex( NULL, 0, cmpServerThread,
NULL, 0, &threadID );
Sleep( 1000 );
/* Connect to the local server */
status = connectCMP( FALSE, TRUE );
waitForThread( hThread );
destroyMutex();
return( status );
}
unsigned __stdcall cmpPKIBootServerThread( void *dummy )
{
pnppkiServer( TRUE, FALSE, FALSE, FALSE );
_endthreadex( 0 );
return( 0 );
}
int testSessionCMPPKIBootClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
#ifndef SERVER_IS_CRYPTLIB
/* 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 the cryptlib "
"CA." );
return( FALSE );
#endif /* !SERVER_IS_CRYPTLIB */
/* Start the server */
createMutex();
hThread = ( HANDLE ) _beginthreadex( NULL, 0, cmpPKIBootServerThread,
NULL, 0, &threadID );
Sleep( 1000 );
/* Connect to the local server with PKIBoot enabled */
status = connectCMP( TRUE, TRUE );
waitForThread( hThread );
destroyMutex();
return( status );
}
unsigned __stdcall cmpPnPPKIServerThread( void *dummy )
{
/* Call with the third parameter set to TRUE to use a chain of CA certs
(i.e. an intermediate CA between the root and end user) rather than
a single CA cert directly issuing the cert to the end user */
pnppkiServer( FALSE, FALSE, FALSE, FALSE );
_endthreadex( 0 );
return( 0 );
}
int testSessionPNPPKIClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
/* Start the server */
createMutex();
hThread = ( HANDLE ) _beginthreadex( NULL, 0, cmpPnPPKIServerThread,
NULL, 0, &threadID );
Sleep( 1000 );
/* Connect to the local server with PKIBoot enabled */
status = connectPNPPKI( FALSE, FALSE, TRUE );
waitForThread( hThread );
destroyMutex();
return( status );
}
unsigned __stdcall cmpPnPPKIDeviceServerThread( void *dummy )
{
/* Call with the third parameter set to TRUE to use a chain of CA certs
(i.e. an intermediate CA between the root and end user) rather than
a single CA cert directly issuing the cert to the end user */
pnppkiServer( FALSE, FALSE, FALSE, TRUE );
_endthreadex( 0 );
return( 0 );
}
int testSessionPNPPKIDeviceClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
/* Start the server */
createMutex();
hThread = ( HANDLE ) _beginthreadex( NULL, 0, cmpPnPPKIDeviceServerThread,
NULL, 0, &threadID );
Sleep( 1000 );
/* Connect to the local server with PKIBoot enabled */
status = connectPNPPKI( FALSE, TRUE, TRUE );
waitForThread( hThread );
destroyMutex();
return( status );
}
unsigned __stdcall cmpPnPPKICaServerThread( void *dummy )
{
pnppkiServer( FALSE, TRUE, FALSE, FALSE );
_endthreadex( 0 );
return( 0 );
}
int testSessionPNPPKICAClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
/* Start the server */
createMutex();
hThread = ( HANDLE ) _beginthreadex( NULL, 0, cmpPnPPKICaServerThread,
NULL, 0, &threadID );
Sleep( 1000 );
/* Connect to the local server with PKIBoot enabled */
status = connectPNPPKI( TRUE, FALSE, TRUE );
waitForThread( hThread );
destroyMutex();
return( status );
}
unsigned __stdcall cmpPnPPKIIntermedCaServerThread( void *dummy )
{
pnppkiServer( FALSE, FALSE, TRUE, FALSE );
_endthreadex( 0 );
return( 0 );
}
int testSessionPNPPKIIntermedCAClientServer( void )
{
HANDLE hThread;
unsigned threadID;
int status;
/* Start the server */
createMutex();
hThread = ( HANDLE ) _beginthreadex( NULL, 0, cmpPnPPKIIntermedCaServerThread,
NULL, 0, &threadID );
Sleep( 1000 );
/* Connect to the local server with PKIBoot enabled */
status = connectPNPPKI( FALSE, FALSE, TRUE );
waitForThread( hThread );
destroyMutex();
return( status );
}
#endif /* WINDOWS_THREADS */
#endif /* TEST_SESSION || TEST_SESSION_LOOPBACK */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -