📄 keyfile.c
字号:
/****************************************************************************
* *
* cryptlib File Keyset Test Routines *
* Copyright Peter Gutmann 1995-2004 *
* *
****************************************************************************/
#include "cryptlib.h"
#include "test/test.h"
#if defined( __MVS__ ) || defined( __VMCMS__ )
/* Suspend conversion of literals to ASCII. */
#pragma convlit( suspend )
#endif /* IBM big iron */
#if defined( __ILEC400__ )
#pragma convert( 0 )
#endif /* IBM medium iron */
/* External flags that 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;
#ifdef TEST_KEYSET
/****************************************************************************
* *
* PGP/PKCS #12 Key Read/Write Tests *
* *
****************************************************************************/
/* Get a public key from a PGP keyring */
static int getPGPPublicKey( const KEYFILE_TYPE keyFileType,
const char *description )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
FILE *filePtr;
const C_STR keysetName = getKeyfileName( keyFileType, FALSE );
int status;
/* If this is the first file read, check that the file actually exists
so we can return an appropriate error message */
if( keyFileType == KEYFILE_PGP )
{
if( ( filePtr = fopen( convertFileName( keysetName ),
"rb" ) ) == NULL )
return( CRYPT_ERROR_FAILED );
fclose( filePtr );
keyReadOK = FALSE;
}
printf( "Testing %s public key read...\n", description );
/* Open the keyset */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
keysetName, 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,
getKeyfileUserID( keyFileType, FALSE ) );
if( cryptStatusError( status ) )
{
printExtError( cryptKeyset, "cryptGetPublicKey()", 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 );
}
printf( "Read of public key from %s keyring succeeded.\n\n",
description );
return( TRUE );
}
int testGetPGPPublicKey( void )
{
if( !getPGPPublicKey( KEYFILE_PGP, "PGP" ) )
return( FALSE );
if( !getPGPPublicKey( KEYFILE_OPENPGP, "OpenPGP (GPG)" ) )
return( FALSE );
if( !getPGPPublicKey( KEYFILE_OPENPGP_HASH, "OpenPGP (GPG/hashed key)" ) )
return( FALSE );
if( !getPGPPublicKey( KEYFILE_OPENPGP_AES, "OpenPGP (GPG/AES-256 key)" ) )
return( FALSE );
if( !getPGPPublicKey( KEYFILE_OPENPGP_RSA, "OpenPGP (GPG/RSA key)" ) )
return( FALSE );
return( getPGPPublicKey( KEYFILE_NAIPGP, "OpenPGP (NAI)" ) );
}
/* Get a private key from a PGP keyring */
static int getPGPPrivateKey( const KEYFILE_TYPE keyFileType,
const char *description )
{
CRYPT_KEYSET cryptKeyset;
CRYPT_CONTEXT cryptContext;
const C_STR keysetName = getKeyfileName( keyFileType, TRUE );
const C_STR password = getKeyfilePassword( keyFileType );
int status;
printf( "Testing %s private key read...\n", description );
/* Open the keyset */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
keysetName, 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,
getKeyfileUserID( keyFileType, TRUE ), 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,
getKeyfileUserID( keyFileType, TRUE ),
password );
}
if( cryptStatusError( status ) )
{
printExtError( cryptKeyset, "cryptGetPrivateKey()", status,
__LINE__ );
return( FALSE );
}
/* Make sure that we can use the key that we've read. We can only do this
with PGP 2.x keys, OpenPGP's peculiar multi-keys identify two (or more)
keys with the same label and we can't specify (at this level) which
key we want to use (the enveloping code can be more specific and so
doesn't run into this problem) */
if( keyFileType == KEYFILE_PGP )
{
status = testCrypt( cryptContext, cryptContext, NULL, FALSE, FALSE );
if( cryptStatusError( status ) )
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 );
}
/* The public and private key reads worked, remember this for later when
we use the keys in other tests */
keyReadOK = TRUE;
printf( "Read of private key from %s keyring succeeded.\n\n",
description );
return( TRUE );
}
int testGetPGPPrivateKey( void )
{
if( !getPGPPrivateKey( KEYFILE_PGP, "PGP" ) )
return( FALSE );
if( !getPGPPrivateKey( KEYFILE_OPENPGP, "OpenPGP (GPG)" ) )
return( FALSE );
if( !getPGPPrivateKey( KEYFILE_OPENPGP_HASH, "OpenPGP (GPG/hashed key)" ) )
return( FALSE );
if( !getPGPPrivateKey( KEYFILE_OPENPGP_AES, "OpenPGP (GPG/AES-256 key)" ) )
return( FALSE );
if( !getPGPPrivateKey( KEYFILE_OPENPGP_RSA, "OpenPGP (GPG/RSA key)" ) )
return( FALSE );
return( getPGPPrivateKey( KEYFILE_NAIPGP, "OpenPGP (NAI)" ) );
}
/* 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( convertFileName( PKCS12_FILE ), "rb" ) ) == NULL )
return( CRYPT_ERROR_FAILED );
fclose( filePtr );
/* puts( "Testing PKCS #12 key read..." ); */
/* Open the keyset */
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
PKCS12_FILE, CRYPT_KEYOPT_READONLY );
if( cryptStatusError( status ) )
{
/* If support for this isn't enabled (which is normal, since the code
doesn't exist), just exit quietly */
if( status == CRYPT_ERROR_PARAM3 )
return( TRUE );
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Get the key - this is currently hardwired to CRYPT_ERROR_NOTAVAIL
after unwrapping the first dozen or so layers of PKCS #12 garbage */
status = cryptGetPrivateKey( cryptKeyset, &cryptContext, CRYPT_KEYID_NAME,
TEXT( "test" ), NULL );
/* if( cryptStatusError( status ) )
{
printExtError( cryptKeyset, "cryptGetPrivateKey()", 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 );
}
/****************************************************************************
* *
* Public/Private Key Read/Write Tests *
* *
****************************************************************************/
/* 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 ) )
{
printExtError( cryptKeyset, "cryptGetPrivateKey()", status,
__LINE__ );
return( FALSE );
}
/* Make sure that we can use the read key */
if( useRSA )
{
status = testCrypt( cryptContext, cryptContext, NULL, FALSE, FALSE );
if( cryptStatusError( status ) )
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 );
}
printf( "Read of %s private key from key file succeeded.\n\n",
useRSA ? "RSA" : "DSA" );
return( TRUE );
}
static int writeFileKey( const BOOLEAN useRSA, const BOOLEAN useAltKeyfile,
const BOOLEAN generateKey )
{
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( generateKey )
{
if( useRSA )
{
status = cryptCreateContext( &privateKeyContext, CRYPT_UNUSED,
CRYPT_ALGO_RSA );
if( cryptStatusOK( status ) )
status = cryptSetAttributeString( privateKeyContext,
CRYPT_CTXINFO_LABEL, RSA_PRIVKEY_LABEL,
paramStrlen( RSA_PRIVKEY_LABEL ) );
}
else
{
status = cryptCreateContext( &privateKeyContext, CRYPT_UNUSED,
CRYPT_ALGO_RSA );
if( cryptStatusOK( status ) )
status = cryptSetAttributeString( privateKeyContext,
CRYPT_CTXINFO_LABEL, DSA_PRIVKEY_LABEL,
paramStrlen( DSA_PRIVKEY_LABEL ) );
}
if( cryptStatusOK( status ) )
status = cryptGenerateKey( privateKeyContext );
if( cryptStatusError( status ) )
return( FALSE );
}
else
{
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,
useAltKeyfile ? TEST_PRIVKEY_ALT_FILE : TEST_PRIVKEY_FILE,
useRSA ? CRYPT_KEYOPT_CREATE : CRYPT_KEYOPT_NONE );
if( cryptStatusError( status ) )
{
if( useAltKeyfile && status == CRYPT_ERROR_PARAM2 )
{
/* If the format isn't supported, this isn't a problem */
puts( "Write of RSA private key to key file skipped.\n" );
return( CRYPT_OK );
}
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
/* Write the key to the file */
status = cryptAddPrivateKey( cryptKeyset, privateKeyContext,
TEST_PRIVKEY_PASSWORD );
if( cryptStatusError( status ) )
{
printExtError( cryptKeyset, "cryptAddPrivateKey()", status,
__LINE__ );
return( FALSE );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -