📄 loadkey.c
字号:
if( isDevice )
status = cryptDeviceCreateContext( cryptDevice, cryptContext,
CRYPT_ALGO_DH );
else
status = cryptCreateContext( cryptContext, CRYPT_UNUSED,
CRYPT_ALGO_DH );
if( cryptStatusError( status ) )
{
printf( "crypt%sCreateContext() failed with error code %d, "
"line %d.\n", isDevice ? "Device" : "", status, __LINE__ );
return( FALSE );
}
if( !setLabel( *cryptContext, "DH key" ) )
{
cryptDestroyContext( *cryptContext );
return( FALSE );
}
if( cryptStatusOK( status ) )
{
#if 1 /* Undefine to test DH keygen */
status = cryptGenerateKey( *cryptContext );
#else
xRESOURCE_DATA msgData;
xsetMessageData( &msgData, ( void * ) dh1024SPKI,
sizeof( dh1024SPKI ) );
#if 0
status = krnlSendMessage( *cryptContext, IMESSAGE_SETATTRIBUTE_S,
&msgData, CRYPT_IATTRIBUTE_KEY_SPKI );
#else
status = cryptDeviceQueryCapability( *cryptContext, 1000,
( CRYPT_QUERY_INFO * ) &msgData );
#endif /* 0 */
#endif /* 0 */
}
if( cryptStatusError( status ) )
{
printf( "DH key load failed, status = %d, line %d.\n", status,
__LINE__ );
cryptDestroyContext( *cryptContext );
return( FALSE );
}
return( TRUE );
}
#endif /* TEST_DH */
static int loadRSAPublicKey( const CRYPT_DEVICE cryptDevice,
CRYPT_CONTEXT *cryptContext,
const C_STR cryptContextLabel,
CRYPT_PKCINFO_RSA *rsaKey,
const BOOLEAN isDevice,
const BOOLEAN useLargeKey )
{
const RSA_KEY *rsaKeyTemplate = useLargeKey ? \
&rsa2048TestKey : &rsa1024TestKey;
int status;
if( isDevice )
status = cryptDeviceCreateContext( cryptDevice, cryptContext,
CRYPT_ALGO_RSA );
else
status = cryptCreateContext( cryptContext, CRYPT_UNUSED,
CRYPT_ALGO_RSA );
if( cryptStatusError( status ) )
{
printf( "crypt%sCreateContext() failed with error code %d, "
"line %d.\n", isDevice ? "Device" : "", status, __LINE__ );
return( status );
}
if( isDevice && !setLabel( *cryptContext, cryptContextLabel ) )
{
cryptDestroyContext( *cryptContext );
return( CRYPT_ERROR_FAILED );
}
cryptInitComponents( rsaKey, CRYPT_KEYTYPE_PUBLIC );
cryptSetComponent( rsaKey->n, rsaKeyTemplate->n, rsaKeyTemplate->nLen );
cryptSetComponent( rsaKey->e, rsaKeyTemplate->e, rsaKeyTemplate->eLen );
status = cryptSetAttributeString( *cryptContext,
CRYPT_CTXINFO_KEY_COMPONENTS, rsaKey,
sizeof( CRYPT_PKCINFO_RSA ) );
cryptDestroyComponents( rsaKey );
if( cryptStatusError( status ) )
cryptDestroyContext( *cryptContext );
return( status );
}
BOOLEAN loadRSAContextsEx( const CRYPT_DEVICE cryptDevice,
CRYPT_CONTEXT *cryptContext,
CRYPT_CONTEXT *decryptContext,
const C_STR cryptContextLabel,
const C_STR decryptContextLabel,
const BOOLEAN useLargeKey,
const BOOLEAN useMinimalKey )
{
CRYPT_PKCINFO_RSA *rsaKey;
const RSA_KEY *rsaKeyTemplate = useLargeKey ? \
&rsa2048TestKey : &rsa1024TestKey;
const BOOLEAN isDevice = ( cryptDevice != CRYPT_UNUSED ) ? TRUE : FALSE;
BOOLEAN loadLargeKey = useLargeKey;
int status;
/* Allocate room for the public-key components */
if( ( rsaKey = ( CRYPT_PKCINFO_RSA * ) malloc( sizeof( CRYPT_PKCINFO_RSA ) ) ) == NULL )
return( FALSE );
/* Some devices only support a single key size that isn't the same as
the built-in one so we adjust the key size being used if necessary */
if( isDevice )
{
CRYPT_QUERY_INFO cryptQueryInfo;
status = cryptDeviceQueryCapability( cryptDevice, CRYPT_ALGO_RSA,
&cryptQueryInfo );
if( cryptStatusError( status ) )
return( FALSE );
if( cryptQueryInfo.keySize != ( rsa1024TestKey.nLen >> 3 ) )
{
if( cryptQueryInfo.keySize != ( rsa2048TestKey.nLen >> 3 ) )
{
printf( "Device requires a %d-bit key, which doesn't "
"correspond to any built-in\ncryptlib key.\n",
cryptQueryInfo.keySize );
return( FALSE );
}
rsaKeyTemplate = &rsa2048TestKey;
loadLargeKey = TRUE;
}
}
/* Create the encryption context */
if( cryptContext != NULL )
{
status = loadRSAPublicKey( cryptDevice, cryptContext,
cryptContextLabel, rsaKey, isDevice,
loadLargeKey );
if( status == CRYPT_ERROR_NOTAVAIL && isDevice )
{
/* The device doesn't support public-key ops, use a native
context for the public key */
puts( " Warning: Device doesn't support public-key operations, "
"using a cryptlib\n native context instead." );
status = loadRSAPublicKey( CRYPT_UNUSED, cryptContext,
cryptContextLabel, rsaKey, FALSE,
loadLargeKey );
}
if( cryptStatusError( status ) )
{
free( rsaKey );
printf( "Public key load failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
if( decryptContext == NULL )
{
/* We're only using a public-key context, return */
free( rsaKey );
return( TRUE );
}
}
/* Create the decryption context */
if( isDevice )
status = cryptDeviceCreateContext( cryptDevice, decryptContext,
CRYPT_ALGO_RSA );
else
status = cryptCreateContext( decryptContext, CRYPT_UNUSED,
CRYPT_ALGO_RSA );
if( cryptStatusError( status ) )
{
free( rsaKey );
if( cryptContext != NULL )
{
cryptDestroyContext( *cryptContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
cryptContextLabel );
}
printf( "crypt%sCreateContext() failed with error code %d, "
"line %d.\n", isDevice ? "Device" : "", status, __LINE__ );
return( FALSE );
}
if( !setLabel( *decryptContext, decryptContextLabel ) )
{
free( rsaKey );
cryptDestroyContext( *decryptContext );
if( cryptContext != NULL )
{
cryptDestroyContext( *cryptContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
cryptContextLabel );
}
return( FALSE );
}
cryptInitComponents( rsaKey, CRYPT_KEYTYPE_PRIVATE );
cryptSetComponent( rsaKey->n, rsaKeyTemplate->n, rsaKeyTemplate->nLen );
cryptSetComponent( rsaKey->e, rsaKeyTemplate->e, rsaKeyTemplate->eLen );
cryptSetComponent( rsaKey->d, rsaKeyTemplate->d, rsaKeyTemplate->dLen );
cryptSetComponent( rsaKey->p, rsaKeyTemplate->p, rsaKeyTemplate->pLen );
cryptSetComponent( rsaKey->q, rsaKeyTemplate->q, rsaKeyTemplate->qLen );
if( !useMinimalKey )
{
cryptSetComponent( rsaKey->u, rsaKeyTemplate->u, rsaKeyTemplate->uLen );
cryptSetComponent( rsaKey->e1, rsaKeyTemplate->e1, rsaKeyTemplate->e1Len );
cryptSetComponent( rsaKey->e2, rsaKeyTemplate->e2, rsaKeyTemplate->e2Len );
}
status = cryptSetAttributeString( *decryptContext,
CRYPT_CTXINFO_KEY_COMPONENTS, rsaKey,
sizeof( CRYPT_PKCINFO_RSA ) );
cryptDestroyComponents( rsaKey );
free( rsaKey );
if( cryptStatusError( status ) )
{
if( cryptContext != NULL )
{
cryptDestroyContext( *cryptContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
cryptContextLabel );
}
cryptDestroyContext( *decryptContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
decryptContextLabel );
printf( "Private key load failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
return( TRUE );
}
BOOLEAN loadRSAContexts( const CRYPT_DEVICE cryptDevice,
CRYPT_CONTEXT *cryptContext,
CRYPT_CONTEXT *decryptContext )
{
return( loadRSAContextsEx( cryptDevice, cryptContext, decryptContext,
RSA_PUBKEY_LABEL, RSA_PRIVKEY_LABEL, FALSE,
FALSE ) );
}
BOOLEAN loadRSAContextsLarge( const CRYPT_DEVICE cryptDevice,
CRYPT_CONTEXT *cryptContext,
CRYPT_CONTEXT *decryptContext )
{
return( loadRSAContextsEx( cryptDevice, cryptContext, decryptContext,
RSA_PUBKEY_LABEL, RSA_PRIVKEY_LABEL, TRUE,
FALSE ) );
}
BOOLEAN loadDSAContextsEx( const CRYPT_DEVICE cryptDevice,
CRYPT_CONTEXT *signContext,
CRYPT_CONTEXT *sigCheckContext,
const C_STR signContextLabel,
const C_STR sigCheckContextLabel )
{
CRYPT_PKCINFO_DLP *dsaKey;
const BOOLEAN isDevice = ( cryptDevice != CRYPT_UNUSED ) ? TRUE : FALSE;
int status;
/* Allocate room for the public-key components */
if( ( dsaKey = ( CRYPT_PKCINFO_DLP * ) malloc( sizeof( CRYPT_PKCINFO_DLP ) ) ) == NULL )
return( FALSE );
/* Create the signature context */
if( signContext != NULL )
{
if( isDevice )
status = cryptDeviceCreateContext( cryptDevice, signContext,
CRYPT_ALGO_DSA );
else
status = cryptCreateContext( signContext, CRYPT_UNUSED,
CRYPT_ALGO_DSA );
if( cryptStatusError( status ) )
{
free( dsaKey );
printf( "cryptCreateContext() failed with error code %d, "
"line %d.\n", status, __LINE__ );
return( FALSE );
}
if( !setLabel( *signContext, signContextLabel ) )
{
free( dsaKey );
cryptDestroyContext( *signContext );
return( FALSE );
}
cryptInitComponents( dsaKey, CRYPT_KEYTYPE_PRIVATE );
cryptSetComponent( dsaKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
cryptSetComponent( dsaKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
cryptSetComponent( dsaKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
cryptSetComponent( dsaKey->x, dlp1024TestKey.x, dlp1024TestKey.xLen );
cryptSetComponent( dsaKey->y, dlp1024TestKey.y, dlp1024TestKey.yLen );
status = cryptSetAttributeString( *signContext,
CRYPT_CTXINFO_KEY_COMPONENTS, dsaKey,
sizeof( CRYPT_PKCINFO_DLP ) );
cryptDestroyComponents( dsaKey );
if( cryptStatusError( status ) )
{
free( dsaKey );
cryptDestroyContext( *signContext );
printf( "Private key load failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
if( sigCheckContext == NULL )
{
free( dsaKey );
return( TRUE );
}
}
/* Create the sig.check context */
if( isDevice )
status = cryptDeviceCreateContext( cryptDevice, sigCheckContext,
CRYPT_ALGO_DSA );
else
status = cryptCreateContext( sigCheckContext, CRYPT_UNUSED,
CRYPT_ALGO_DSA );
if( cryptStatusError( status ) )
{
free( dsaKey );
if( signContext != NULL )
{
cryptDestroyContext( *signContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
signContextLabel );
}
printf( "cryptCreateContext() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
if( !setLabel( *sigCheckContext, sigCheckContextLabel ) )
{
if( signContext != NULL )
{
cryptDestroyContext( *signContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
signContextLabel );
}
cryptDestroyContext( *sigCheckContext );
return( FALSE );
}
cryptInitComponents( dsaKey, CRYPT_KEYTYPE_PUBLIC );
cryptSetComponent( dsaKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
cryptSetComponent( dsaKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
cryptSetComponent( dsaKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
cryptSetComponent( dsaKey->y, dlp1024TestKey.y, dlp1024TestKey.yLen );
status = cryptSetAttributeString( *sigCheckContext,
CRYPT_CTXINFO_KEY_COMPONENTS, dsaKey,
sizeof( CRYPT_PKCINFO_DLP ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -