📄 testkey.c
字号:
/****************************************************************************
* *
* cryptlib Keyset Test Routines *
* Copyright Peter Gutmann 1995-2001 *
* *
****************************************************************************/
#ifdef _MSC_VER
#include "../cryptlib.h"
#include "../test/test.h"
#else
#include "cryptlib.h"
#include "test/test.h"
#endif /* Braindamaged MSC include handling */
/* External flags which indicate that the key read/update routines worked OK.
This is set by earlier self-test code, if it isn't set some of the tests
are disabled */
extern int keyReadOK, doubleCertOK;
/****************************************************************************
* *
* Keyset Access Routines Test *
* *
****************************************************************************/
/* Get a public key from a PGP keyring */
int testGetPGPPublicKey( void )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
FILE *filePtr;
int status;
/* Check that the file actually exists so we can return an appropriate
error message */
if( ( filePtr = fopen( PGP_PUBKEY_FILE, "rb" ) ) == NULL )
return( CRYPT_ERROR_FAILED );
fclose( filePtr );
keyReadOK = FALSE;
puts( "Testing PGP public key read..." );
/* Try and open the keyset and try to read the required key */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
PGP_PUBKEY_FILE, CRYPT_KEYOPT_READONLY );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Get the key */
status = cryptGetPublicKey( cryptKeyset, &cryptContext, CRYPT_KEYID_NAME,
"test" );
if( cryptStatusError( status ) )
{
printf( "cryptGetPublicKey() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
cryptDestroyContext( cryptContext );
/* Close the keyset */
status = cryptKeysetClose( cryptKeyset );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetClose() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
puts( "Read of public key from PGP keyring succeeded.\n" );
return( TRUE );
}
/* Get a private key from a PGP keyring */
int testGetPGPPrivateKey( void )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
FILE *filePtr;
int status;
/* Check that the file actually exists so we can return an appropriate
error message */
if( ( filePtr = fopen( PGP_PRIVKEY_FILE, "rb" ) ) == NULL )
return( CRYPT_ERROR_FAILED );
fclose( filePtr );
puts( "Testing PGP private key read..." );
/* Try and open the keyset and try to read the required key */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
PGP_PRIVKEY_FILE, CRYPT_KEYOPT_READONLY );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Get the key. First we try it without a password, if that fails we
retry it with the password - this tests a lot of the private-key get
functionality including things like key cacheing */
status = cryptGetPrivateKey( cryptKeyset, &cryptContext, CRYPT_KEYID_NAME,
"test10", NULL );
if( status == CRYPT_ERROR_WRONGKEY )
{
/* We need a password for this private key, get it from the user and
get the key again */
status = cryptGetPrivateKey( cryptKeyset, &cryptContext,
CRYPT_KEYID_NAME, "test10", "test10" );
}
if( cryptStatusError( status ) )
{
printf( "cryptGetPrivateKey() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
cryptDestroyContext( cryptContext );
/* Close the keyset */
status = cryptKeysetClose( cryptKeyset );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetClose() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Both key reads worked, remember this for later */
keyReadOK = TRUE;
puts( "Read of private key from PGP keyring succeeded.\n" );
return( TRUE );
}
/* Get a public key from an OpenPGP keyring */
int testGetOpenPGPPublicKey( void )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
FILE *filePtr;
int status;
/* Check that the file actually exists so we can return an appropriate
error message */
if( ( filePtr = fopen( OPENPGP_PUBKEY_FILE, "rb" ) ) == NULL )
return( CRYPT_ERROR_FAILED );
fclose( filePtr );
keyReadOK = FALSE;
puts( "Testing OpenPGP public key read..." );
/* Try and open the keyset and try to read the required key */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
OPENPGP_PUBKEY_FILE, CRYPT_KEYOPT_READONLY );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Get the key */
status = cryptGetPublicKey( cryptKeyset, &cryptContext, CRYPT_KEYID_NAME,
"test" );
if( cryptStatusError( status ) )
{
printf( "cryptGetPublicKey() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
cryptDestroyContext( cryptContext );
/* Close the keyset */
status = cryptKeysetClose( cryptKeyset );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetClose() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
puts( "Read of public key from OpenPGP keyring succeeded.\n" );
return( TRUE );
}
/* Get a private key from an OpenPGP keyring */
int testGetOpenPGPPrivateKey( void )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
FILE *filePtr;
int status;
/* Check that the file actually exists so we can return an appropriate
error message */
if( ( filePtr = fopen( OPENPGP_PRIVKEY_FILE, "rb" ) ) == NULL )
return( CRYPT_ERROR_FAILED );
fclose( filePtr );
puts( "Testing OpenPGP private key read..." );
/* Try and open the keyset and try to read the required key */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
OPENPGP_PRIVKEY_FILE, CRYPT_KEYOPT_READONLY );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Get the key. First we try it without a password, if that fails we
retry it with the password - this tests a lot of the private-key get
functionality including things like key cacheing */
status = cryptGetPrivateKey( cryptKeyset, &cryptContext, CRYPT_KEYID_NAME,
"test", NULL );
if( status == CRYPT_ERROR_WRONGKEY )
{
/* We need a password for this private key, get it from the user and
get the key again */
status = cryptGetPrivateKey( cryptKeyset, &cryptContext,
CRYPT_KEYID_NAME, "test", "test1" );
}
if( cryptStatusError( status ) )
{
printf( "cryptGetPrivateKey() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
cryptDestroyContext( cryptContext );
/* Close the keyset */
status = cryptKeysetClose( cryptKeyset );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetClose() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Both key reads worked, remember this for later */
keyReadOK = TRUE;
puts( "Read of private key from OpenPGP keyring succeeded.\n" );
return( TRUE );
}
/* Get a key from a PKCS #12 file. Because of the security problems
associated with this format, the code only checks the data format but
doesn't try to read or use the keys. If anyone wants this, they'll
have to add the code themselves. Your security warranty is void if you
implement this */
int testGetBorkenKey( void )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
FILE *filePtr;
int status;
/* Check that the file actually exists so we can return an appropriate
error message */
if( ( filePtr = fopen( PKCS12_FILE, "rb" ) ) == NULL )
return( CRYPT_ERROR_FAILED );
fclose( filePtr );
/* puts( "Testing PKCS #12 key read..." ); */
/* Try and open the keyset and try to read the required key */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
PKCS12_FILE, CRYPT_KEYOPT_READONLY );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Get the key - this is currently hardwired to CRYPT_ERROR_FAILED after
unwrapping the first dozen or so layers of PKCS #12 garbage */
status = cryptGetPrivateKey( cryptKeyset, &cryptContext, CRYPT_KEYID_NAME,
"test", NULL );
/* if( cryptStatusError( status ) )
{
printf( "cryptGetPrivateKey() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
cryptDestroyContext( cryptContext ); */
/* Close the keyset */
status = cryptKeysetClose( cryptKeyset );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetClose() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* puts( "Read of key from PKCS #12 file succeeded.\n" ); */
return( TRUE );
}
/* Read/write a private key from a file */
static int readFileKey( const BOOLEAN useRSA )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
int status;
printf( "Testing %s private key read from key file...\n", useRSA ? "RSA" : "DSA" );
/* Open the file keyset */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
TEST_PRIVKEY_FILE, CRYPT_KEYOPT_READONLY );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Read the key from the file */
status = cryptGetPrivateKey( cryptKeyset, &cryptContext,
CRYPT_KEYID_NAME,
useRSA ? RSA_PRIVKEY_LABEL : DSA_PRIVKEY_LABEL,
TEST_PRIVKEY_PASSWORD );
if( cryptStatusError( status ) )
{
printf( "cryptGetPrivateKey() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Close the keyset */
status = cryptKeysetClose( cryptKeyset );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetClose() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
cryptDestroyContext( cryptContext );
printf( "Read of %s private key from key file succeeded.\n\n",
useRSA ? "RSA" : "DSA" );
return( TRUE );
}
static int writeFileKey( const BOOLEAN useRSA )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT privateKeyContext;
int status;
printf( "Testing %s private key write to key file...\n", useRSA ? "RSA" : "DSA" );
/* Create the private key context */
if( useRSA )
{
if( !loadRSAContexts( CRYPT_UNUSED, NULL, &privateKeyContext ) )
return( FALSE );
}
else
if( !loadDSAContexts( CRYPT_UNUSED, &privateKeyContext, NULL ) )
return( FALSE );
/* Create/open the file keyset. For the first call (with RSA) we create
a new keyset, for subsequent calls we update the existing keyset */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
TEST_PRIVKEY_FILE,
useRSA ? CRYPT_KEYOPT_CREATE : CRYPT_KEYOPT_NONE );
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -